analitics

Pages

Saturday, March 4, 2017

Working with datetime python module.

This module is very good and I worked with this issue by using MySQL and python.
The version of python I used is 2.7 under Fedora distro.
You can take a look at the official page.
I use the pip and not the DNF fedora Linux tool.
 
[root@localhost lucru]# pip install datetime
Collecting datetime
Downloading DateTime-4.1.1.zip (66kB)
100% |████████████████████████████████| 71kB 703kB/s 
Collecting zope.interface (from datetime)
Downloading zope.interface-4.3.3.tar.gz (150kB)
100% |████████████████████████████████| 153kB 2.2MB/s 
Collecting pytz (from datetime)
Downloading pytz-2016.10-py2.py3-none-any.whl (483kB)
100% |████████████████████████████████| 491kB 2.4MB/s 
Requirement already satisfied: setuptools in /usr/lib/python2.7/site-packages (from zope.interface->datetime)
Installing collected packages: zope.interface, pytz, datetime
Running setup.py install for zope.interface ... done
Running setup.py install for datetime ... done
Successfully installed datetime-4.1.1 pytz-2016.10 zope.interface-4.3.3

I solve this problem:
  • conversion using the lambda function
    parser.add_argument('date', type=lambda s: datetime.datetime.strptime(s, '%Y-%m-%d'))
  • solve last day
    datetime.datetime.strptime(new_value, '%Y-%m-%d %H:%M:%S')-timedelta(days=1)
  • print the today date
    print date.today()
  • show date using an explicit format string
    today=date.today()
    today.strftime("%A %d. %B %Y")
    'Sunday 05. March 2017'
    
  • using epoch issue [1]
    from datetime import datetime
    now_epoch = (datetime.utcnow() - datetime(1970, 1, 1)).total_seconds()
    datetime.utcfromtimestamp(now_epoch)
    datetime.datetime(2017, 3, 4, 22, 35, 13, 463409)
    datetime.fromtimestamp(now_epoch)
    datetime.datetime(2017, 3, 5, 0, 35, 13, 463409)
    import pytz
    datetime.fromtimestamp(now_epoch, pytz.utc)
    datetime.datetime(2017, 3, 4, 22, 35, 13, 463409, tzinfo=)
    
[1] The Unix epoch is the time 00:00:00 UTC on 1 January 1970. There is a problem with this definition, in that UTC did not exist in its current form until 1972;

Using pygeoip and maxmin database.


I try to locate one IP using the databases from maxmind website and is not good for me.
The database records show me the output from country area.
I read the docs from here.
This is the python script I used:
#wget -N -q http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
import pygeoip 
gip = pygeoip.GeoIP('GeoLiteCity.dat')
rec = gip.record_by_addr('___________________')
for key,val in rec.items():
    print "%s: %s" %(key,val)