Very good explanation about GIL
Affichage des articles dont le libellé est python. Afficher tous les articles
Affichage des articles dont le libellé est python. Afficher tous les articles
mardi 21 juin 2022
jeudi 20 juin 2019
[Python] Singleton
Multiple ways :
- Way 1
class MyClass:
def __init__(self):
pass
@classmethod
def get(cls):
try:
return cls.instance
except AttributeError:
cls.instance = MyClass()
return cls.instance
- Way 2
this = sys.modules[__name__]
class MyClass:
def __init__(self):
pass
def get_my_singleton():
try:
return this.my_singleton
except AttributeError:
this.my_singleton = MyClass()
return this.my_singleton
More details about this way: https://stackoverflow.com/a/35904211
try:
return this.my_singleton
except AttributeError:
this.my_singleton = MyClass()
return this.my_singleton
More details about this way: https://stackoverflow.com/a/35904211
lundi 10 juin 2019
[Python] Difference between package and module
A module is a file :-) Modules are used to keep logically related code functions/classes together.
A package is a folder containing a set of modules.
A package may contain sub-packages.
A package must have a __init__.py at the root of the directory. When a package is imported, this __init__.py file is implicitly executed. https://docs.python.org/3/reference/import.html#regular-packages
Packaging help you to keep logically related set of modules together just as modules are used to keep logically related code functions/classes together.
With Python 3.3 and later, a package is called a regular package, and a new type of package appears : namspace package. https://docs.python.org/3/reference/import.html#namespace-packages
A package is a folder containing a set of modules.
A package may contain sub-packages.
A package must have a __init__.py at the root of the directory. When a package is imported, this __init__.py file is implicitly executed. https://docs.python.org/3/reference/import.html#regular-packages
Packaging help you to keep logically related set of modules together just as modules are used to keep logically related code functions/classes together.
With Python 3.3 and later, a package is called a regular package, and a new type of package appears : namspace package. https://docs.python.org/3/reference/import.html#namespace-packages
mardi 17 octobre 2017
File upload with Tornado
Demonstrates a server that receives a multipart-form-encoded set of files in an HTTP POST, or streams in the raw data of a single file in an HTTP PUT.
AND
Demonstrates uploading files to a server, without concurrency. It can either POST a multipart-form-encoded request containing one or more files, or PUT a single file without encoding.
https://github.com/tornadoweb/tornado/tree/master/demos/file_upload
AND
Demonstrates uploading files to a server, without concurrency. It can either POST a multipart-form-encoded request containing one or more files, or PUT a single file without encoding.
https://github.com/tornadoweb/tornado/tree/master/demos/file_upload
mercredi 29 juillet 2015
[Python] Buffered ouput by default
By default, Python produces buffered output (when you use print).
This could be a problem when you would like to see in "real time" your print(...) for example in a log file.
You can disable the buffer for output, by adding the option "-u" like that :
python -u my_script.py
I had this problem with Tornado + supervisor. I did not understand why my print(..) was not written immediately in my log file => -u option solves my problem !
This could be a problem when you would like to see in "real time" your print(...) for example in a log file.
You can disable the buffer for output, by adding the option "-u" like that :
python -u my_script.py
I had this problem with Tornado + supervisor. I did not understand why my print(..) was not written immediately in my log file => -u option solves my problem !
mardi 27 janvier 2015
[Python] Tornado - send POST parameters in the body with AsyncHTTPClient
If you want to send POST data to an URL(a RESTful web service for example) with AsyncHTTPClient, you can do that :
import urllibLink : http://stackoverflow.com/questions/10367981/how-to-use-post-method-in-tornado
post_data = { 'data': 'test data' } #A dictionary of your post data
body = urllib.urlencode(post_data) #Make it into a post request
http_client = tornado.httpclient.AsyncHTTPClient()
http_client.fetch("http://127.0.0.1:8888", handle_request, method='POST', headers=None, body=body)
mardi 28 octobre 2014
vendredi 24 octobre 2014
[Python] pip install : gcc: error: unrecognized command line option '-mno-cygwin'
If you are on Windows and get this message :
Then you must :
- edit or create C:\PythonXX\Lib\distutils\cygwinccompiler.py and remove any traces of -mno-cygwin
- open an new cmd and type again your pip install greenlet (or another lib)
Have fun
:o)
gcc: error: unrecognized command line option '-mno-cygwin'when you type pip install greenlet (or another lib)
Then you must :
- edit or create C:\PythonXX\Lib\distutils\cygwinccompiler.py and remove any traces of -mno-cygwin
- open an new cmd and type again your pip install greenlet (or another lib)
Have fun
:o)
mercredi 20 août 2014
[Python] How to check if an object has an attribute : hasattr vs __dict__
class A(object):
foo = 1
class B(A):
pass
b = B()
print( hasattr(b, 'foo') )
True
print( 'foo' in b.__dict__)
False
hasattr : checks on super class
__dict__ : does not include super class
foo = 1
class B(A):
pass
b = B()
print( hasattr(b, 'foo') )
True
print( 'foo' in b.__dict__)
False
hasattr : checks on super class
__dict__ : does not include super class
jeudi 8 mai 2014
WeasyPrint : Type str doesn't support the buffer API
Under Windows, if you type weasyprint http://www.google.com test.pdf and get this message :
File "C:\Python34\lib\site-packages\weasyprint\__init__.py", line 293, in _select_source
base_url = path2url(filename)
File "C:\Python34\lib\site-packages\weasyprint\urls.py", line 87, in path2url
path = pathname2url(path)
File "C:\Python34\lib\nturl2path.py", line 46, in pathname2url
if not ':' in p:
TypeError: Type str doesn't support the buffer API
You must edit C:\PythonXX\Lib\site-packages\weasyprint\urls.py and comment these lines :
if isinstance(path, unicode):
path = path.encode(FILESYSTEM_ENCODING)
[Python] pip install : expecting string instruction after `rep'
If you are on Windows and get this message :
Then you must :
- verify under the MinGW Installation Manager that you have only these two packages installed : mingw32-base et mingw32-gcc-g++
- type under cmd : mingw-get upgrade binutils
- open an new cmd and type again your pip install greenlet (or another lib)
Have fun
:o)
Error: expecting string instruction after `rep'when you type pip install greenlet (or another lib)
Then you must :
- verify under the MinGW Installation Manager that you have only these two packages installed : mingw32-base et mingw32-gcc-g++
- type under cmd : mingw-get upgrade binutils
- open an new cmd and type again your pip install greenlet (or another lib)
Have fun
:o)
[Python] pip install : unable to find vcvarsall.bat
If you are on Windows and get this message :
Then you must :
- install MinGW (after the installation, run the MinGW Installation Manager and install only these two packages : mingw32-base et mingw32-gcc-g++)
- add the mingw's bin directory to your path
- edit or create C:\PythonXX\Lib\distutils\distutils.cfg and write :
- open an new cmd and type again your pip install greenlet (or another lib)
Have fun
:o)
Unable to find vcvarsall.batwhen you type pip install greenlet (or another lib)
Then you must :
- install MinGW (after the installation, run the MinGW Installation Manager and install only these two packages : mingw32-base et mingw32-gcc-g++)
- add the mingw's bin directory to your path
- edit or create C:\PythonXX\Lib\distutils\distutils.cfg and write :
[build]
compiler=mingw32
- open an new cmd and type again your pip install greenlet (or another lib)
Have fun
:o)
lundi 27 janvier 2014
[Bottle + Tornado] From a Bottle application, sending parameters to the Tornado server
When you use Bottle + Tornado (or CherryPy,... etc...), you can give options to the Tornado server, by declaring them inside the Bottle run(...) method.
Here hello_bottle.py :
from bottle import route, run, template
import os
@route('/hello/<name>')
def index(name):
return template('<b>Hello {{name}}</b>!', name=name)
data_dir='/'
#we give ssl_options to the Tornado server :
run(host='localhost', port=8080,server='tornado',ssl_options={
"certfile": os.path.join(data_dir, "mydomain.crt"),
"keyfile": os.path.join(data_dir, "mydomain.key"),
})
Here hello_bottle.py :
from bottle import route, run, template
import os
@route('/hello/<name>')
def index(name):
return template('<b>Hello {{name}}</b>!', name=name)
data_dir='/'
#we give ssl_options to the Tornado server :
run(host='localhost', port=8080,server='tornado',ssl_options={
"certfile": os.path.join(data_dir, "mydomain.crt"),
"keyfile": os.path.join(data_dir, "mydomain.key"),
})
And when we execute :
python hello_bottle.py
Bottle v0.13-dev server starting up (using TornadoServer(ssl_options={'keyfile': '/mydomain.key', 'certfile': '/mydomain.crt'}))...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.
Voila :o)
Source :
SSL with Tornado :
Sending parameters to CherryPy:
Servers allowed with Bottle :
mercredi 14 août 2013
[Python] convert timestamp -> datetime -> timestamp
#convert a date to a timestamp
def convert_to_timestamp(dt):
return calendar.timegm(dt.utctimetuple())
#convert a timestamp to a datetime
def convert_to_date(timestamp_long):
return datetime.datetime.utcfromtimestamp(timestamp_long)
mercredi 19 juin 2013
Bottle Restful - Links
http://nongraphical.com/2012/08/using-bottle-py-in-production/
http://www.marginhound.com/bottle-py-resources/
http://myadventuresincoding.wordpress.com/2011/01/02/creating-a-rest-api-in-python-using-bottle-and-mongodb/
http://mark-kirby.co.uk/2013/creating-a-true-rest-api/
http://gotofritz.net/blog/weekly-challenge/restful-python-api-bottle/
lundi 14 janvier 2013
CherryPy and Python 3 : Port not bound / _Timer / _Event
Hi all,
I have recently updated my Python 3 and my CherryPy to the latest versions.
After that, different problems occur during the startup process of my application(using CherryPy):
These issues are marked as "resolved" ... but in fact they are resolved in the release 3.2.3.
This release 3.2.3 is not easily accessible :
- not present on the CherryPy website on the "Download" section
- not present on the CherryPy bitbucket wiki
=>On the "Download" section and on the bitbucket wiki, the last version is the 3.2.2 :(
For downloading the 3.2.3, you must go to the project on bitbucket.org and click on "Downloads"
Tada :)
I have recently updated my Python 3 and my CherryPy to the latest versions.
After that, different problems occur during the startup process of my application(using CherryPy):
- OSError - Port XXXX not bound : bug logged in JIRA
- 'module' object has no attribute '_Timer' (same problem with _Event) : bug logged in JIRA
These issues are marked as "resolved" ... but in fact they are resolved in the release 3.2.3.
This release 3.2.3 is not easily accessible :
- not present on the CherryPy website on the "Download" section
- not present on the CherryPy bitbucket wiki
=>On the "Download" section and on the bitbucket wiki, the last version is the 3.2.2 :(
For downloading the 3.2.3, you must go to the project on bitbucket.org and click on "Downloads"
Tada :)
jeudi 6 septembre 2012
Python decorator
Hi all !
This is a very interesting tutorial about decorator in Python :
Python Decorators I: Introduction to Python Decorators
Python Decorators II: Decorator Arguments
Python Decorators III: A Decorator-Based Build System
This is a very interesting tutorial about decorator in Python :
Python Decorators I: Introduction to Python Decorators
Python Decorators II: Decorator Arguments
Python Decorators III: A Decorator-Based Build System
mercredi 29 août 2012
Who uses cherrypy and web2py ? [updated]
I'm a little afraid on the future of CherryPy because :
- there are no new commits since April
- new issues are not assigned since April too
- the page on cherrypy.org detailing the websites running with CherryPy is very poor : http://docs.cherrypy.org/stable/appendix/success.html
- the last commits has been made only by Jason R. Coombs (only 1 person !)
Then I ask me some questions and I have some serious doubts.
Maybe it s time to choose a new framework... Maybe web2py. Why web2py ? Because when I look at this page I'm very impressed by the number of websites using web2py.
But it seems that the project is only maintained by Massimo Di Pierro (the creator). Bad point :o(
In conclusion : if you know a robust framework for Python 3, tell me ! :o)
[update] I realize that web2py doesn't support Python 3 :o(
Flask doesn't support too.
[update] Bottle supports Python 3. Not a lot of commits, but issues are maintained/followed.
[update] Pyramid seems to be a serious framework (maintained by few members). And it is "Python 3 ready" !
[update] I realize that web2py doesn't support Python 3 :o(
Flask doesn't support too.
[update] Bottle supports Python 3. Not a lot of commits, but issues are maintained/followed.
[update] Pyramid seems to be a serious framework (maintained by few members). And it is "Python 3 ready" !
dimanche 19 août 2012
IDE pour Python 3
Pour pouvoir développer en Python 3, il y a plein d'IDEs.
Il y a IDLE (super basique), PyDev avec Eclipse (200Mo à télécharger), etc...
Et il y a PyScripter. Super léger (6Mo), pas aussi complet que PyDev, mais en tout cas plus complet qu'IDLE : complétion de code, debugger, permet de gérer des "projets", interfaçage avec CVS/SVN, pydoc, etc...
Inscription à :
Articles (Atom)
Categories
- /etc/hosts (1)
- 443 (1)
- 80 (1)
- a2dp (1)
- addsubview (1)
- affix (1)
- amazon (1)
- android (6)
- angularjs (1)
- angularjs cross domain json post (1)
- angularjs ionic ng-click twice (1)
- animate (1)
- ansible (2)
- antlr (1)
- apache2 processes (1)
- app (1)
- app store (1)
- apple (1)
- appstore (1)
- avis (2)
- baignoire (1)
- basics (1)
- bitbucket (1)
- blackberry (1)
- bluetooth (1)
- booster (1)
- bootstrap (3)
- bottle (4)
- browsers (1)
- buffer (1)
- cherrypy (3)
- chromebook real life (1)
- coder (2)
- communicator (1)
- config.txt (1)
- creme chocolat (1)
- crepes bretonnes (1)
- crlf (1)
- css (2)
- cuisine (1)
- database (4)
- datatables (1)
- datetime (1)
- delete (1)
- disconnect (1)
- distributing (1)
- english (2)
- flask (1)
- fontawesome (1)
- francais (51)
- futuristic (1)
- game (1)
- gil (1)
- git (9)
- github (1)
- gratuit (2)
- hadopi (1)
- header (1)
- height zero (1)
- hibernate (1)
- hotel (2)
- http (1)
- https (2)
- ionic (2)
- ios (2)
- ios7 (1)
- iOS8 (1)
- iphone (1)
- jaune (1)
- java (7)
- javamelody (1)
- javascript (1)
- json (3)
- kindle (1)
- knockout (3)
- leaflet (1)
- legere (1)
- lf (1)
- life cycle (1)
- linkedin resume builder profile (1)
- linux (9)
- log (1)
- luxembourg (1)
- machine (1)
- maizena (1)
- minecraft (1)
- mobile (1)
- mongo (1)
- mongodb (3)
- mongodb mongo linux (1)
- multiple (1)
- myspace (1)
- mysql (1)
- netbeans cordova android cordova-plugin-file (1)
- nginx (2)
- nintendo (1)
- number (1)
- opensolaris (1)
- openstreetmap (1)
- oracle (15)
- order by (1)
- output (1)
- overclocking (1)
- packaging (1)
- parameters (1)
- personnaliser bootstrap (1)
- photos (1)
- pip (2)
- pipewire (1)
- prime (1)
- problem (2)
- project (1)
- publish (1)
- python (26)
- raspberry pi (2)
- raspi-config (1)
- recette (1)
- redirect (1)
- restful (2)
- schema (1)
- screen (1)
- screencast (1)
- script (1)
- scrollspy (1)
- serialization (1)
- sessions (2)
- shell (1)
- shutdown reboot linux reinstall apt-get (1)
- smartgwt (1)
- sncf (1)
- software (1)
- spring (1)
- sql (1)
- sticky footer (1)
- supervisor (1)
- systeme (1)
- tile (1)
- timestamp (1)
- title (1)
- tornado (3)
- turbo (1)
- ubuntu (2)
- uialertview (1)
- video capture (1)
- viewdidappear (1)
- viewdidload (1)
- viewwillappear (1)
- weasyprint (1)
- web (1)
- web2py (1)
- windows (5)
- worker (1)
- xbox wireless headset (1)
- xdk barcode scanner intel.xdk.device.barcode.scan (1)
- XDK rename project (1)
- xeno galaxies (1)