This article is participating in the Python Theme Month, check out the detailsActive link

preface

In fact, I do not want to write this out, after all, I feel that I have not done too much.

As a result, during the Python theme month, the test student saw it and said, “Why don’t you write an article about the problem you helped me solve last time?”

I replied, because there’s not much technology involved.

Test students heard me answer, refuted, also experience and results, fast, give ye write, Python code I send you.

And so there was this one.

demand

In fact, the requirement is as shown in the screenshot above, which is to do a lot of fake data, but do not know that the test should be such a good one. Students asked me who do iOS development.

Because sometimes I think about faker some data and do some simple data debugging through Moya, so I don’t know much about this.

In fact, I was deeply impressed by what Shimmering Kim said to Red A — you are A faker!

So you see, knowing more about ACG does help with the association, fog XD

The code!

So without further ado, go straight to the code:

import faker import time def fakeData(): fake = faker.Faker(locale='zh_CN') jasperId = ["101842918", "101200123", "101842000", "1112311342", "123422323"] date = "20210318" start = time.time() for i in range(len(jasperId)): For j in range(10000) select * from j in range(10000); iccid = fake.random_number(digits = 20) sim = fake.random_number(digits = 11) msisdn = "86" + str(sim) imsi = "4600" + str(sim) string = jasperId[i] + "\t" + date + "\t" + str(iccid) + "\t"+ imsi + "\t" + msisdn + "\t" + "10\n" with open ("esim.txt", "a+") as fp: Write (string) totalTime = time.time() -start print(" {}".format(totalTime)) if __name__ == "__main__": fakeData()Copy the code

By the way, I also collected the elapsed time:

Close to 9s time, did about 5W data, efficiency is not bad.

Code I am directly looking for test students to me, in fact, I think so many for loop, use lamada isn’t it sweet?

Other uses of Faker extend thinking

As mentioned above, the TargetType protocol of Moya in Swift has a sampleData. We can finally convert the Data generated in faker framework into Swift Data to facilitate testing.

public protocol TargetType {

    /// The target's base `URL`.
    var baseURL: URL { get }

    /// The path to be appended to `baseURL` to form the full `URL`.
    var path: String { get }

    /// The HTTP method used in the request.
    var method: Moya.Method { get }

    /// Provides stub data for use in testing.
    var sampleData: Data { get }

    /// The type of HTTP task to be performed.
    var task: Task { get }
    
    /// The type of validation to perform on the request. Default is `.none`.
    var validationType: ValidationType { get }

    /// The headers to be used in the request.
    var headers: [String: String]? { get }

}
Copy the code

In addition, we can set up our own back-end service locally for debugging App network requests.

Or upload the data to a dedicated Mock site and use the Mock site’s Api to debug network requests.

Refer to the article

Python library – Faker

Stop manipulating the data, Faker