I’ve been reading a lot about python-way lately so my question is How to do dependency injection python-way? I am talking about usual scenarios when, for example, service A needs access to UserService for authorization checks. Answer It all depends on the situation. For example, if you use dependency in…
Tag: python
List Directories and get the name of the Directory
I am trying to get the code to list all the directories in a folder, change directory into that folder and get the name of the current folder. The code I have so far is below and isn’t working at the minute. I seem to be getting the parent folder name. I also have other files in the folder but
Can’t download YouTube video
I’m having trouble retrieving the YouTube video automatically. Here’s the code. The problem is the last part. download = urllib.request.urlopen(download_url).read() There’s an error message (thanks Wooble): Answer The code on the original question relies on several assumptions about the content of YouTube pag…
How can I read a function’s signature including default argument values?
Given a function object, how can I get its signature? For example, for: I would like to get “my_method(first, second, third=’something’)”. Answer Python 3.5+ recommends inspect.signature().
What version of Visual Studio is Python on my computer compiled with?
I am trying to find out the version of Visual Studio that is used to compile the Python on my computer It says What I do not understand is this MSC V.1500 designation. Does it mean it is compiled with Visual Studio 2005? I cannot find this information on http://python.org. Answer Visual C++ version _MSC_VER V…
Best way to do enum in Sqlalchemy?
I’m reading about sqlalchemy and I saw following code: Should I make ‘type’ an int, with constants in a library? Or should I make just make type an enum? Answer SQLAlchemy has an Enum type since 0.6: http://docs.sqlalchemy.org/en/latest/core/type_basics.html?highlight=enum#sqlalchemy.types.E…
How to generate a random number with a specific amount of digits?
Let’s say I need a 3-digit number, so it would be something like: Answer You can use either of random.randint or random.randrange. So to get a random 3-digit number: * Assuming you really meant three digits, rather than “up to three digits”. To use an arbitrary number of digits: Output:
Inexpensive ways to add seek to a filetype object
PdfFileReader reads the content from a pdf file to create an object. I am querying the pdf from a cdn via urllib.urlopen(), this provides me a file like object, which has no seek. PdfFileReader, however uses seek. What is the simple way to create a PdfFileReader object from a pdf downloaded via url. Now, what…
Using __str__ representation for printing objects in containers
I’ve noticed that when an instance with an overloaded __str__ method is passed to the print function as an argument, it prints as intended. However, when passing a container that contains one of those instances to print, it uses the __repr__ method instead. That is to say, print(x) displays the correct …
SQLAlchemy: a better way for update with declarative?
Let’s say I have a user table in declarative mode: When I know user’s id without object loaded into session, I update such user like this: I dislike using User.__table__, should I stop worrying with that? Is there a better way to do this? Answer There’s also some update capability at the ORM…