Huge numbers to 128 bit hex

Im trying to convert big number, a really big number more then 1000 digits, I want it to be hex 128bit .

Tried using this :

def tohex(val, 128):
  return hex((val + (1 << 128)) % (1 << 128))

but with big numbers all I get is something like this :

0x0L

hex(x) function does not work also.

in the end I need to convert a really big number in to 32digits hex (128bit) any suggestions guys?

Copied example does not working in IDLE, python

I am trying to learn python for Machine Learning and I just copied an example from the test book to my IDEL program. Here is the code, copied from here

from numpy import *
import operator

def createDataSet():
    group = array([[1.0,1.1], [1.0,1.0], [0,0], [0,0.1]])
    labels = ['A','A','B','B']
    return group, labels

def classify0(inX, dataSet, labels, k):
    dataSetSize = dataSet.shape[0]

    diffMat = tile(inX, (dataSetSize,1)) - dataSet
    sqDiffMat = diffMat ** 2
    sqDistances = sqDiffMat.sum(axis = 1)
    distances = sqDistances ** 0.5

    sortedDistIndicies = distances.argsort()

    # Voting with lowest k distances
    classCount={}
    for i in range(k):
        voteIlabel = labels[sortedDistIndicies[i]]
        classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1

    # Decompose into a list of tuples and sort by second item
    sortedClassCount = sorted(classCount.iteritems(),
        key=operator.itemgetter(1), reverse=True)

    return sortedClassCount[0][0]

group, labels = createDataSet()
classify0([0,0], group, labels, 3)

Then I compel it in IDLE but I am keep getting the following error message in Python Shell. (I am using version 3.4.3)

The error message is:

Traceback (most recent call last):
  File "/Users/Python/KNN.py", line 32, in <module>
    classify0([0,0], group, labels, 3)
  File "/Users/Python/KNN.py", line 26, in classify0
    sortedClassCount = sorted(classCount.itemgetter(),
AttributeError: 'dict' object has no attribute 'itemgetter'

I just don't understand why this is not working...and I don't even see where this 'dict' object is...Please help!

Django/Apache & virtualenv on Ubuntu not using the right Python interpreter

I'm running a small Django server on Ubuntu with virtualenv, apache2 and mod_wsgi.

I have virtualenv created with python3 on both windows and linux. pip --version shows:

(env)lbr@irma-fr:/var/www/irmaweb/irmaweb$ pip --version
pip 7.0.3 from /var/www/irmaweb/env/lib/python3.4/site-packages (python 3.4)

It works fine on windows, but when deploying on my Ubuntu server i get the following error:

Django Error

The error is because Django create a new empty DB in /var/www instead of using the DB created by ./manage.py migrate in my project root:

(env)lbr@irma-fr:/var/www/irmaweb/irmaweb$ ls -al /var/www
total 20
drwxrws---  5 lbr      www-data 4096 Jul  1 05:58 .
drwxr-xr-x 12 root     root     4096 Jun 26 09:53 ..
-rw-r--r--  1 www-data www-data    0 Jul  1 05:58 dev.db

But I think the root of the issue is because Django get called from python2 as shown in the screenshot instead of getting called by python3 as my virtualenv is configured for.

Here is below my apache2 configuration:

(env)lbr@irma-fr:/var/www/irmaweb/irmaweb$ cat /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
WSGIDaemonProcess irmaweb python-path=/var/www/irmaweb/irmaweb:/var/www/irmaweb/env/lib/python3.4/site-packages
WSGIProcessGroup irmaweb
WSGIScriptAlias / /var/www/irmaweb/irmaweb/irmaweb/wsgi.py
Alias /site_media/static/ /var/www/irmaweb/irmaweb/irmaweb/site_media/static/

So I guess I'm missing something! how to tell apache/wsgi to call python from my virtualenv folder:

(env)lbr@irma-fr:/var/www/irmaweb$ ls -al env/bin/python*
lrwxrwxrwx 1 lbr www-data       7 Jun 30 15:19 env/bin/python -> python3
-rwxrwxr-x 1 lbr www-data 4061272 Jun 30 15:19 env/bin/python3
lrwxrwxrwx 1 lbr www-data       7 Jun 30 15:19 env/bin/python3.4 -> python3

Instead of using the system python from /usr/bin ?

Can't open video using opencv

The opencv works fine when doing other things. It can can open images and show images. But it can't open a video.

The code I'm using to open a video is as below

import cv2

cap = cv2.VideoCapture("MOV_0006.mp4")

while True:
    ret, frame = cap.read()

    cv2.imshow('video', frame)
    if cv2.waitKey(1) & 0xff == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

But when executing, it outputs error message like below

[h264 @ 0x1053ba0] AVC: nal size 554779904
[h264 @ 0x1053ba0] AVC: nal size 554779904
[h264 @ 0x1053ba0] no frame!

My vlc and mplayer can play this video, but the opencv can't.

I have installed x264 and libx264-142 codec package. (using sudo apt-get install)

My version of ubuntu is 14.04 trusty.

I'm not sure is it a codec problem or not?

I have rebuilt opencv either with WITH_UNICAP=ON or with WITH_UNICAP=OFF, but it doesn't affect the problem at all. The error message never changes.

how to get image from lmdb by python?

and what is the data type of that result, does that data type match the function open('xx.jpg').read()

my purpose is to read image from lmdb and then match my code (that code is written for data type of open('xx.jpg').read() ),so I want to get the same data type from lmdb if I can

guzzle 6 post method is not working

i am using yii2 and guzzle version 4 when for api pyhton Django 1.5 when i am trying to post data with postman is working but when i am trying with php is saying bad request 404

error is GuzzleHttp\Exception\ClientException
v1/jobs/ [status code] 400 [reason phrase] BAD REQUEST

post man format data working data format

url -vm.xxx.com/v1/jobs/
 {
      "job_position":"Test Postion",
      "eligibility_course_id":["1","2","3","4"],
      "eligibiltiy_course_ids":["1","2","3","4"],
      "eligibility_branch_id":["5","6","7","8"],
      "eligibiltiy_skill_ids":["10","23","24"],
      "eligibiltiy_sublocation_ids":["10","23","24"],
      "location":["26","30","2","3"],
      "job_category":["1","4","7"],
      "sms_category":"1",
      "posting_date":"31\/07\/2014",
      "expiry_date":"31\/08\/2014",
      "job_type_id":"1",
      "job_type_other":"",
      "hiring_process":["1"],
      "job_description":"Test Description<\/p>",
      "company_name":"Test Company name",
      "company_profile":"Test company profile<\/p>",
      "company_url":"",
      "seo_title":"Test Seo Title",
      "seo_description":"Test Seo Description",
      "seo_keyword":"Test seo Keyward",
      "response":"1","how_to_apply":"",
      "terms_agreement":"1",
      "specify_terms_agreement":"",
      "employer_id":"1008369",
      "button_submitForm":"Post Job",
      "posted_by_id":"114",
      "template_name":"job_description"
    }

with php code method -1

$arr=[
"job_position" => "Test Postion",
"eligibility_course_id" => ["1","2","3","4"],
"eligibiltiy_course_ids" => ["1","2","3","4"],
"eligibility_branch_id" => ["5","6","7","8"],
"eligibiltiy_skill_ids" => ["10","23","24"],
"eligibiltiy_sublocation_ids" => ["10","23","24"],
"location" => ["26","30","2","3"],
"job_category" => ["1","4","7"],
"sms_category" => "1",
"posting_date" => "31\/07\/2014",
"expiry_date" => "31\/08\/2014",
"job_type_id" => "1",
"job_type_other" => "",
"hiring_process" => ["1"],
"job_description" => "Test Description<\/p>",
"company_name" => "Test Company name",
"company_profile" => "Test company profile<\/p>",
"company_url" => "",
"seo_title" => "Test Seo Title",
"seo_description" => "Test Seo Description",
"seo_keyword" => "Test seo Keyward",
"response" => "1","how_to_apply" => "",
"terms_agreement" => "1",
"specify_terms_agreement" => "",
"employer_id" => "1008369",
"button_submitForm" => "Post Job",
"posted_by_id" => "114",
"template_name" => "job_description"
];
$client = new Client();
$apiurl=Yii::$app->params['apiurl'];
$url=$apiurl.'/v1/jobs/';
try {

       $response = $client->post($url, ['json' => $arr]);
}
catch (\GuzzleHttp\Exception\BadResponseException $e) {

    $req = $e->getRequest();
    $resp =$e->getResponse();
    echo "<pre>";
    var_dump($req);
    var_dump($resp);
    die('ss');
}

try method-2

 $arr=json_encode($arr);
$request = $client->post($url,array(
    'content-type' => 'application/json'
),array());
$request->setBody($arr); #set body!
$response = $request->send();

then i am getting

InvalidArgumentException
No method is configured to handle the content-type config key

(Python) Why isn't my for loop reading the last couple lines?

I'm just a beginner so I'm practicing basics and I know indentation is key and all that when writing in python but even though my indentation is correct (hopefully), it's reading it as if it's outside the loop. I know nested loops and crud make things all frizzy but I don't understand why when my indentation is fine.

f=open("output.txt","r+")
abc = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
inp = raw_input("Enter a number between 0 and 27 inclusive: ")
for i in range(int(inp)):
    print " "
    c=0
    s=""
    while c<i and c<len(abc):
        s+= abc[c]+" "
        c+=1
    c-=2
    while c>=0:
        s+= abc[c]+" "
        c-=1
    f.write(s)
    print s
print "\nAll done"
f.close()

(Ps if some of the logic is out of whack sorry. All its supposed to do is go down the alphabet like a "a aba abcba abcdcba" and so on) Thank you


Ok so it works now but I'm trying to change it to an exec file but once I did, now when I double click to run it on the console it says: line 1: syntax error near unexpected token (' line 1: f = open("output.txt","w")' logout (ps I added a f.close() and fixed the logic) but when I run it regularly on the console (aka just "python filename.py" it runs just fine. Please help :)