text
stringlengths
226
34.5k
Why are the subject and content empty when using python to send Email? Question: I tried to run the following python script (copied from <http://www.tutorialspoint.com/python/python_sending_email.htm>) on a remote server to send an Email. I successfully received the Email in my mailbox, but unfortunately the content is always empty. Another problem is, if I slightly change the message within """From: xxx """, say delete this part `From: sender Name`, then I could even receive the empty Email. What is the magic going on inside message line """xxx"""? import smtplib def send_Email2(): sender = 'username@company.com' receivers = "receivername@company.com" message = """From: Sender Name <username@company.com> To: Receiver Name <receivername@company.com> Subject: SMTP e-mail test This is a test e-mail message. """ try: smtpObj = smtplib.SMTP('1xx.128.2.xxx',25) smtpObj.sendmail(sender, receivers, message) print "Successfully sent email" except SMTPException: print "Error: unable to send email" Answer: Have you tried like below: message['Subject'] = 'SMTP e-mail test' message['From'] = 'username@company.com' message['To'] = 'receivername@company.com'
Keras output looks different than others Question: When I run the below code : from keras.models import Sequential from keras.layers import Dense import numpy import time # fix random seed for reproducibility seed = 7 numpy.random.seed(seed) # load dataset dataset = numpy.loadtxt("C:/Users/AQader/Desktop/Keraslearn/mammm.csv", delimiter=",") # split into input (X) and output (Y) variables X = dataset[:,0:5] Y = dataset[:,5] # create model model = Sequential() model.add(Dense(50, input_dim=5, init='uniform', activation='relu')) model.add(Dense(25, init='uniform', activation='tanh')) model.add(Dense(15, init='uniform', activation='tanh')) model.add(Dense(1, init='uniform', activation='sigmoid')) # Compile model model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # Fit the model model.fit(X, Y, nb_epoch=200, batch_size=20, verbose = 0) time.sleep(0.1) # evaluate the model scores = model.evaluate(X, Y) print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100)) I end up receiving the following. 32/829 [>.............................] - ETA: 0sacc: 84.20% That's it. Only one line that shows up after a half minute of training. After looking through other questions, the usual output looks like: Epoch 1/20 1213/1213 [==============================] - 0s - loss: 0.1760 Epoch 2/20 1213/1213 [==============================] - 0s - loss: 0.1840 Epoch 3/20 1213/1213 [==============================] - 0s - loss: 0.1816 Epoch 4/20 1213/1213 [==============================] - 0s - loss: 0.1915 Epoch 5/20 1213/1213 [==============================] - 0s - loss: 0.1928 Epoch 6/20 1213/1213 [==============================] - 0s - loss: 0.1964 Epoch 7/20 1213/1213 [==============================] - 0s - loss: 0.1948 Epoch 8/20 1213/1213 [==============================] - 0s - loss: 0.1971 Epoch 9/20 1213/1213 [==============================] - 0s - loss: 0.1899 Epoch 10/20 1213/1213 [==============================] - 0s - loss: 0.1957 Can anyone tell me what may be wrong here? I am a beginner in this yet this doesn't seem normal. Please note that there are no errors in the "code" sections. What I mean is that the 0sacc is what shows up. I'm running this in the Anaconda Environment Python 2.7 on a Windows 7 64-bit machine. 8GB RAM and Core i5 5th gen. Answer: By calling `model.fit` with `verbose = 0` you have suppressed detailed output. Try setting `verbose = 1`.
Python Django Web Application issue Question: I have created a user creation page and forget password page. all the fields are created in models.py and i want to show the forgot password through HttpResponse when the user clicks of submit button forgot password page. Please help how to get it. models.py as in my project from django.db import models # Create your models here. class NewUser(models.Model): fname=models.CharField(max_length=12) lname=models.CharField(max_length=15) address=models.TextField() email=models.EmailField(max_length=50) pwd=models.CharField(max_length=12) cpwd=models.CharField(max_length=12) cell=models.CharField(max_length=10) def __unicode__(self): return self.email views.py as in my project def forgot_pwd(request): if request.method=='POST': #import pdb; pdb.set_trace() email_id=request.POST.get('userid','') print email_id rec_mail_id=NewUser.objects.get(email=email_id) print rec_mail_id if rec_mail_id: rec_pwd=NewUser.objects.get(pwd) return HttpResponse("Your password is %s" %rec_pwd) else: return HttpResponse("You are not yet registered\n <a href=/CreateUser>Please click here to register</a>") else: return render_to_response('forgot_pwd.html', context_instance=RequestContext(request)) Answer: `models.py` should be : class user_registration(models.Model): email_id = models.EmailField(max_length=500,blank=False) password = models.CharField(max_length=500,null=True) `views.py` should be : def checkRegistration(request): if request.method == 'GET': form = ContactForm(request.GET) if form.is_valid(): email = form.cleaned_data['email_id'] getpassword = user_registration.objects.get(email_id=email) print getpassword.password "Manuculate with getpassword as you want" else: "Do whatever you want" `form.py` should be : from django.forms import ModelForm from models import data class ContactForm(ModelForm): class Meta: model = user_registration fields = [ "email_id", "password" ]
How to delete a file with invalid name using python? Question: I've got a `file` named `umengchannel_316_豌豆荚` I want to delete this file.. I tried the following: `os.remove()`, `os.unlink()` , `shutil.move()` but nothing seems to work.. Are there any other approaches to this problem? Answer: I'm using Unix OS, have been able to create a blank file with the name you specified and delete it with `os.remove()` in python intepreter. $ cd ~ $ touch "umengchannel_316_豌豆荚.txt" $ python >>> import os >>> os.remove("/home/neko/umengchannel_316_豌豆荚.txt")
Extract last string between brackets from a string - python Question: I would like to know the way for extracting the last string between brackets from a long string. So I need a function, `extract_last`, for example, that let me get an output like that: >> extract_last('(hello) my (name) is (Luis)') >> 'Luis' How can I accomplish that without using a for, I am looking for the smartest way. The for way that I have implemented works. I have no tested it with all the possibilities, but the simplest things do it well: def extract_last(string): bracket_found = False characters = [] for character in string[::-1]: if character == ')': bracket_found = True continue if(character == '(' and bracket_found): break; if(bracket_found and character != ')'): characters.append(character) return ''.join(characters[::-1]) But this solution has many lines and I know that using regular expressions or something like that, I could accomplish that with a one-or-two-lines solution. Answer: import re def extract_last(val): r = re.findall(r'\((\w+)\)', val) return r[-1] if r else None
Unable to import boto3 library after installation Question: I installed boto3 library from AWS SDK but when I try to import in python interpreter, I get error. Here is the traceback: > > > import boto3 Traceback (most recent call last): File "", line 1, in File > "/home/rahul/rahul/boto3/boto3/**init**.py", line 16, in from boto3.session > import Session File "/home/rahul/rahul/boto3/boto3/session.py", line 17, in > import botocore.session ImportError: No module named 'botocore' Can you please help me fix this issue? Answer: Apparently you have installed boto3 but not botocore botocore is the basis of boto3 but is lower level See <https://pypi.python.org/pypi/botocore>
Python optimize fmin gives A value in x_new is above the interpolation range error Question: I am trying to interpolate the minimum of a quadratic function of which I have three samples. The first test in the code snippet below works. It gives: [![test 1](http://i.stack.imgur.com/tzTYq.png)](http://i.stack.imgur.com/tzTYq.png) In the second test there are two similar values. The minimum of my quadratic function should lie in between. However, I get the following error "A value in x_new is above the interpolation range." Does anybody know a way how to solve this. from matplotlib import pyplot as plt from scipy import interpolate from scipy import optimize def test(x, y): xmin, ymin = getMin(x, y) plt.figure() plt.plot(x, y, 'o-r', xmin, ymin, 'bx') def getMin(x, y): f = interpolate.interp1d(x, y, kind="quadratic") xmin = optimize.fmin(lambda x: f(x), x[1]) ymin = f(xmin) return xmin[0], ymin[0] test([18, 19, 20], [-34.3, -74.3, -7.3]) test([18, 19, 20], [-34.3, -74.3, -74.2]) Answer: Analytically works like a charm def getMinFit(x, y): p = np.polyfit(x, y, 2) xmin = -p[1]/(2*p[0]) ymin = np.polyval(p, xmin) return xmin, ymin [![enter image description here](http://i.stack.imgur.com/PfyQq.png)](http://i.stack.imgur.com/PfyQq.png)
Fast way to select from numpy array without intermediate index array Question: Given the following 2-column array, I want to select items from the second column that correspond to "edges" in the first column. This is just an example, as in reality my `a` has potentially millions of rows. So, ideally I'd like to do this as fast as possible, and without creating intermediate results. import numpy as np a = np.array([[1,4],[1,2],[1,3],[2,6],[2,1],[2,8],[2,3],[2,1], [3,6],[3,7],[5,4],[5,9],[5,1],[5,3],[5,2],[8,2], [8,6],[8,8]]) i.e. I want to find the result, desired = np.array([4,6,6,4,2]) which is entries in `a[:,1]` corresponding to where `a[:,0]` changes. One solution is, b = a[(a[1:,0]-a[:-1,0]).nonzero()[0]+1, 1] which gives `np.array([6,6,4,2])`, I could simply prepend the first item, no problem. However, this creates an intermediate array of the indexes of the first items. I could avoid the intermediate by using a list comprehension: c = [a[i+1,1] for i,(x,y) in enumerate(zip(a[1:,0],a[:-1,0])) if x!=y] This also gives `[6,6,4,2]`. Assuming a generator-based `zip` (true in Python 3), this doesn't need to create an intermediate representation and should be very memory efficient. However, the inner loop is not numpy, and it necessitates generating a list which must be subsequently turned back into a numpy array. Can you come up with a numpy-only version with the memory efficiency of `c` but the speed efficiency of `b`? Ideally only one pass over `a` is needed. (Note that measuring the speed won't help much here, unless `a` is very big, so I wouldn't bother with benchmarking this, I just want something that is theoretically fast and memory efficient. For example, you can assume rows in `a` are streamed from a file and are slow to access -- another reason to avoid the `b` solution, as it requires a second random-access pass over `a`.) Edit: a way to generate a large `a` matrix for testing: from itertools import repeat N, M = 100000, 100 a = np.array(zip([x for y in zip(*repeat(np.arange(N),M)) for x in y ], np.random.random(N*M))) Answer: I am afraid if you are looking to do this in a vectorized way, you can't avoid an intermediate array, as there's no built-in for it. Now, let's look for vectorized approaches other than `nonzero()`, which might be more performant. Going by the same idea of performing differentiation as with the original code of `(a[1:,0]-a[:-1,0])`, we can use boolean indexing after looking for non-zero differentiations that correspond to "edges" or shifts. Thus, we would have a vectorized approach like so - a[np.append(True,np.diff(a[:,0])!=0),1] **Runtime test** The original solution `a[(a[1:,0]-a[:-1,0]).nonzero()[0]+1,1]` would skip the first row. But, let's just say for the sake of timing purposes, it's a valid result. Here's the runtimes with it against the proposed solution in this post - In [118]: from itertools import repeat ...: N, M = 100000, 2 ...: a = np.array(zip([x for y in zip(*repeat(np.arange(N),M))\ for x in y ], np.random.random(N*M))) ...: In [119]: %timeit a[(a[1:,0]-a[:-1,0]).nonzero()[0]+1,1] 100 loops, best of 3: 6.31 ms per loop In [120]: %timeit a[1:][np.diff(a[:,0])!=0,1] 100 loops, best of 3: 4.51 ms per loop Now, let's say you want to include the first row too. The updated runtimes would look something like this - In [123]: from itertools import repeat ...: N, M = 100000, 2 ...: a = np.array(zip([x for y in zip(*repeat(np.arange(N),M))\ for x in y ], np.random.random(N*M))) ...: In [124]: %timeit a[np.append(0,(a[1:,0]-a[:-1,0]).nonzero()[0]+1),1] 100 loops, best of 3: 6.8 ms per loop In [125]: %timeit a[np.append(True,np.diff(a[:,0])!=0),1] 100 loops, best of 3: 5 ms per loop
How to get http request from curl? Question: I've got a curl request which looks like: curl -s http://someurl1.com -H 'Accept: application/json' -u 'emv:LgKrAVkFf2c6mr4DFBQUdjBK' -d grant_type='password' -d scope='offline_access' -d username='datafiles@random.com' -d password='456Qwer()' -d acr_values='tenant:sdfdfsdf-e5e7-42d5-9881-sdfsdf' | python -m json.tool How can I construct a simple HTTP request from it? I've tried to import it with postman but got `Error while importing Curl: Zero or Multiple option-less arguments. Only one is supported (the URL)` Answer: curl http://someurl1.com -H 'Accept: application/json' -u 'emv:LgKrAVkFf2c6mr4DFBQUdjBK' -d grant_type='password' -d scope='offline_access' -d username='datafiles@random.com' -d password='456Qwer()' -d acr_values='tenant:sdfdfsdf-e5e7-42d5-9881-sdfsdf' this string can be imported by Postman.
How do I force these ttk apps to resize when I resize the window? Question: I have comments in a number of places where I've tried to use the columnconfigure or rowconfigure methods to cause the windows to resize, but none of it seems to be working. I can also take out mainloop() and my program runs exactly the same. This leads me to believe I'm perhaps not calling/using mainloop correctly. The methods are mostly just for organization, and I know they're not reusable methods right now. I was planning on maybe fixing that at some point, but that's not the purpose of this post. #!/usr/bin/python from tkinter import * from tkinter import ttk import webbrowser from dictionary import * class AI(): def __init__(self): self.urls = dictionary.get() self.makeMainframe() self.makeLabels() self.makeDropDown() self.makeButton() self.makeEntry() for child in self.mainframe.winfo_children(): child.grid_configure(padx=2, pady=2) #child.columnconfigure(index='all',weight=1) #child.rowconfigure(index='all',weight=1) def openMenu(self): webbrowser.open(self.urls[self.lunchVar.get()]) print(self.urls[self.lunchVar.get()]) def saveChoice(self, event): print(self.foodVar.get()) def makeMainframe(self): self.mainframe = ttk.Frame(padding="3 3 12 12") self.mainframe.grid(column=0, row=0, sticky=(N,W,E,S)) #self.mainframe.columnconfigure('all', weight=1) #self.mainframe.rowconfigure(index='all', weight=1) def makeDropDown(self): self.lunchVar = StringVar() self.lunchChoice = ttk.Combobox(self.mainframe, textvariable=self.lunchVar, state='readonly') self.lunchChoice['values'] = list(self.urls.keys()) self.lunchChoice.grid(column=2, row=1, sticky=(W, E)) def makeLabels(self): ttk.Label(self.mainframe, text="Today's lunch is from...").grid(column=1, row=1, sticky=(E,W)) ttk.Label(self.mainframe, text="Click here for a menu -->").grid(column=1, row=2, sticky=(E,W)) ttk.Label(self.mainframe, text="What will you be having?").grid(column=1, row=3, sticky=(E,W)) def makeButton(self): self.menu = ttk.Button(self.mainframe, textvariable=self.lunchVar, command=self.openMenu) self.menu.grid(column=2, row=2, sticky=(W, E)) def makeEntry(self): self.foodVar = StringVar() self.foodChoice = ttk.Entry(self.mainframe, textvariable=self.foodVar) self.foodChoice.grid(column=2, row=3, sticky=(E,W)) self.foodChoice.bind("<Return>", self.saveChoice) #self.foodChoice.columnconfigure(index='all', weight=1) AI=AI() mainloop() Answer: You place `self.mainframe` in the root window, but you never give any rows and columns in the root window a weight. You also never explicitly create a root window, though that isn't contributing to the problem. Also, `"all"` isn't a valid row or column. The first step is to get your main frame to resize, then you can focus on the contents inside the frame. def makeMainframe(self): ... self.mainframe._root().columnconfigure(0, weight=1) self.mainframe._root().rowconfigure(index=0, weight=1) Note: in the case where you're putting a single frame inside some other widget (such as the root window), `pack` is arguably a better choice because you don't have to remember to give weight to the rows and columns. You can replace this: self.mainframe.grid(column=0, row=0, sticky=(N,W,E,S)) self.mainframe._root().columnconfigure(0, weight=1) self.mainframe._root().rowconfigure(index=0, weight=1) ... with this: self.mainframe.pack(fill="both", expand=True) Assuming that you want the entry widgets to expand to fill the space horizontally, you need to do the same with `mainframe`: def makeMainframe(self): ... self.mainframe.grid_columnconfigure(2, weight=1)
Pyspark Handle Ctrl + Z? Question: When running pyspark, if you do a `Ctrl` \+ `Z` in the middle of a process, it stops the job from running but does not close SparkContext. This means the port is not closed either and spark has to jump through several ports to connect the next time someone runs a spark job. I want to prevent people from using `Ctrl` \+ `Z` while running these scripts, but the standard python handler doesn't seem to work when SparkContext is running, which I assume is a result of the fact that it's technically running in java and I've only set a handler for python? Here is a sample of what I've written. import signal from pyspark import SparkContext sc = SparkContext() def handler(signum, frame): print 'Please refrain from using CTRL + Z, use CTRL + C instead' signal.signal(signal.SIGTSTP, handler) while True: pass When I run this code, pressing `Ctrl` \+ `Z` still does the default behavior of pausing rather than executing my handler function. This code does work the way I want it to if I eliminate the spark content as so: import signal def handler(signum, frame): print 'Please refrain from using CTRL + Z, use CTRL + C instead' signal.signal(signal.SIGTSTP, handler) while True: pass Answer: What I ended up doing was adding `trap "" SIGTSTP` to my spark-submit shell script just before the spark-submit.
how can I parse json with a single line python command? Question: I would like to use python to parse JSON in batch scripts, for example: HOSTNAME=$(curl -s ${HOST} | python ?) Where the JSON output from curl looks like: '{"hostname":"test","domainname":"example.com"}' How can I do this with a single line python command? Answer: Based on the JSON below being returned from the curl command ... '{"hostname":"test","domainname":"example.com"}' You can then use python to extract the hostname using the python json module: HOSTNAME=$(curl -s ${HOST} | python -c \ 'import json,sys;print json.load(sys.stdin)["hostname"]') Note that I have split the line using a `\` to make it more readable on stackoverflow. I've also simplified the command based on [chepner's](http://stackoverflow.com/users/1126841/chepner) comment. Original source: [Parsing JSON with UNIX tools](http://stackoverflow.com/questions/1955505/parsing-json-with-unix- tools) See also: <https://wiki.python.org/moin/Powerful%20Python%20One-Liners>
jira-python hanging on request on Windows without any kind of failure or notification Question: My script returns changelog history for `JIRA` tickets, and it seems to work fine on my dev machine (Mac Pro). It hung a few times when I tried to implement async to pull the requests faster, but with a single threaded process it works every time. When I deployed on our Windows production server, it reaches about the 90% completion point, and then hangs without any kind of helpful message or indication what might be going wrong. The Windows Task Scheduler shows it as "complete", which means that it must be returning some kind of successful completion code that isn't outwardly visible. I'm a bit confused as to where to even start tracking down the cause of this issue. I'll include my code for reference: # jira_changelog_history.py """ Records the history for every jira issue ID in a database. """ from concurrent.futures import ThreadPoolExecutor from csv import DictWriter import datetime import gzip import logging from threading import Lock from typing import Generator from jira import JIRA from inst_config import config3, jira_config as jc from inst_utils import aws_utils from inst_utils.inst_oauth import SigMethodRSA from inst_utils.jira_utils import JiraOauth from inst_utils.misc_utils import ( add_etl_fields, clean_data, get_fieldnames, initialize_logger ) TODAY = datetime.date.today() logger = initialize_logger(config3.GET_LOGFILE( # r'C:\Runlogs\JiraChangelogHistory\{date}.txt'.format( # date=TODAY # ) 'logfile.txt' ) ) def return_jira_keys( jira_instance: JIRA, jql: str, result_list: list, start_at: int, max_res: int = 500 ) -> Generator: issues = jira_instance.search_issues( jql_str=jql, startAt=start_at, maxResults=max_res, fields='key' ) for issue in issues: result_list.append(issue.key) def write_issue_history( jira_instance: JIRA, issue_id: str, writer: DictWriter, lock: Lock): logging.debug('Now processing data for issue {}'.format(issue_id)) changelog = jira_instance.issue(issue_id, expand='changelog').changelog for history in changelog.histories: created = history.created for item in history.items: to_write = dict(issue_id=issue_id) to_write['date'] = created to_write['field'] = item.field to_write['changed_from'] = item.fromString to_write['changed_to'] = item.toString clean_data(to_write) add_etl_fields(to_write) with lock: writer.writerow(to_write) if __name__ == '__main__': try: signature_method = SigMethodRSA(jc.JIRA_RSA_KEY_PATH) o = JiraOauth(jc.OAUTH_URLS, jc.CONSUMER_INFO, signature_method) req_pub = o.oauth_dance_part1() o.gain_authorization(jc.AUTHORIZATION_URL, req_pub) acc_pub, acc_priv = o.oauth_dance_part2() with open(jc.JIRA_RSA_KEY_PATH) as key_f: key_data = key_f.read() oauth_dict = { 'access_token': acc_pub, 'access_token_secret': acc_priv, 'consumer_key': config3.CONSUMER_KEY, 'key_cert': key_data } j = JIRA( server=config3.BASE_URL, oauth=oauth_dict ) # Full load # jql = 'project not in ("IT Service Desk")' # 3 day load, need SQL statement to trunc out if key in jql = 'project not in ("IT Service Desk") AND updatedDate > -3d' # "total" attribute of JIRA.ReturnedList returns the total records total_records = j.search_issues(jql, maxResults=1).total logging.info('Total records: {total}'.format(total=total_records)) start_at = tuple(range(0, total_records, 500)) keys = [] with ThreadPoolExecutor(max_workers=5) as exec: for start in start_at: exec.submit(return_jira_keys, j, jql, keys, start) table = r'ods_jira.staging_jira_changelog_history' fieldnames = get_fieldnames( table_name=table, db_info=config3.REDSHIFT_POSTGRES_INFO_PROD ) # loadfile = ( # r'C:\etl3\file_staging\jira_changelog_history\{date}.csv.gz'.format( # date=TODAY # )) loadfile = r'jira_changelogs.csv.gz' with gzip.open(loadfile, 'wt') as outf: writer = DictWriter( f=outf, fieldnames=fieldnames, delimiter='|', extrasaction='ignore' ) writer_lock = Lock() for index, key in enumerate(keys): logging.info( 'On #{num} of {total}: %{percent_done:.2f} ' 'completed'.format( num=index, total=total_records, percent_done=(index / total_records) * 100 )) write_issue_history( jira_instance=j, issue_id=key, writer=writer, lock=writer_lock ) # with ThreadPoolExecutor(max_workers=3) as exec: # for key in keys: # exec.submit( # write_issue_history, # j, # key, # writer, # writer_lock # ) s3 = aws_utils.S3Loader( infile=loadfile, s3_filepath='jira_scripts/changelog_history/' ) s3.load() rs = aws_utils.RedshiftLoader( table_name=table, safe_load=True ) delete_stmt = ''' DELETE FROM {table_name} WHERE issue_id in {id_list} '''.format( table_name=table, id_list=( '(' + ', '.join(['\'{}\''.format(key) for key in keys]) + ')') ) rs.execute( rs.use_custom_sql, sql=delete_stmt ) rs.execute( rs.copy_to_db, copy_from=s3.get_full_destination() ) except Exception as e: raise Answer: I'd suggest a single worker to see if that works better
Interactive Python - solutions for relative imports Question: From [Python relative imports for the billionth time](http://stackoverflow.com/questions/14132789/python-relative-imports-for- the-billionth-time): * For a `from .. import` to work, the module's name must have at least as many dots as there are in the `import` statement. * ... if you run the interpreter interactively ... the name of that interactive session is `__main__` * Thus you cannot do relative imports directly from an interactive session I like to use interactive Jupyter Notebook sessions to explore data and test modules before writing production code. To make things clear and accessible to teammates, I like to place the notebooks in an `interactive` package located alongside the packages and modules I am testing. package/ __init__.py subpackage1/ __init__.py moduleX.py moduleY.py moduleZ.py subpackage2/ __init__.py moduleZ.py interactive/ __init__.py my_notebook.ipynb During an interactive session in `interactive.my_notebook.ipynb`, how would you import other modules like `subpackage1.moduleX` and `subpackage2.moduleZ`? Answer: The solution I currently use is to append the parent package to `sys.path`. import sys sys.path.append("/Users/.../package/") import subpackage1.moduleX import subpackage2.moduleZ
unconverted data remains python strptime Question: I am trying to convert strings to datetime in python, but I am getting an error "unconverted data remains: 11". The strings I am dealing with have a form like "Dec\xa031 2011". I think the unicode character is causing a problem. I have tried splitting on \xa0 which gives me ['Dec', '31 2011'], then joining. I have also tried replacing by re with re.sub('\xa0', '', dateStr). Neither has worked. Answer: That works for your example: import datetime s = "Dec\xa031 2011" datetime.datetime.strptime(s, "%b\xa0%d %Y") Outputs: datetime.datetime(2011, 12, 31, 0, 0)
Generate an image tag with a Django static url in the view instead of the template Question: I want to generate an image with a static url in a view, then render it in a template. I use `mark_safe` so that the HTML won't be escaped. However, it still doesn't render the image. How do I generate the image tag in Python? from django.shortcuts import render from django.utils.safestring import mark_safe def check(request): html = mark_safe('<img src="{% static "brand.png" %}" />') return render(request, "check.html", {"image": html}) Rendering it in a template: {{ image }} renders the img tag without generating the url: <img src="{% static "brand.png" %} /> Answer: You've marked the string, containing a template directive, as safe. However, templates are rendered in one pass. When you render that string, that's it, it doesn't go through and then render any directives that were in rendered strings. You need to [generate the static url](http://stackoverflow.com/a/17738606/400617) without using template directives. from django.contrib.staticfiles.templatetags.staticfiles import static url = static('images/deal/deal-brand.png') img = mark_safe('<img src="{url}">'.format(url=url))
Is it possible to get the name of AWS EMR step currently executing without going to console Question: If there are 3 steps that are executed in EMR, is it possible to know which step is currently executing, using shell script. I am able to get current state of the EMR cluster, using this: `aws emr describe-cluster --cluster-id ${cluster_id} | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["Cluster"]["Status"]["State"]'` but I am not able to get the information about the name of step currently executing, is it possible. Answer: The `list-steps` option in the AWS CLI for EMR returns each step. It can be filtered to only return the steps in a certain state: `aws emr list-steps --step-states "RUNNING"` (See <http://docs.aws.amazon.com/cli/latest/reference/emr/list-steps.html>)
How can i select a file with python? Question: [enter image description here](http://i.stack.imgur.com/mMIsq.png) I can do like this on Java, but i can't do on Python StringSelection ss = new StringSelection("C:\\Users\\Mert\\Desktop\\hello.png"); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null); Robot robot = new Robot(); robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); Answer: You can open a file GUI similar to Java using Python's `tkinter`: from Tkinter import Tk from tkFileDialog import askopenfilename Tk().withdraw() filename = askopenfilename() print(filename) And the Python3 equivalent: from tkinter.filedialog import askopenfilename filename = askopenfilename()
fail to visualize a network in python - issue with pygraphviz? Question: As I use some code to visualize a network, I encountered an error Traceback (most recent call last): File "networkplot.py", line 21, in <module> pos = graphviz_layout(G, prog='neato') File "/opt/conda/lib/python2.7/site-packages/networkx/drawing/nx_agraph.py", line 228, in graphviz_layout return pygraphviz_layout(G,prog=prog,root=root,args=args) File "/opt/conda/lib/python2.7/site-packages/networkx/drawing/nx_agraph.py", line 258, in pygraphviz_layout 'http://pygraphviz.github.io/') ImportError: ('requires pygraphviz ', 'http://pygraphviz.github.io/') I therefore download `pygraphiviz-1.3.1.tar.gz` and `pip install pygraphviz`. It showed Failed building wheel for pygraphviz Running setup.py clean for pygraphviz Failed to build pygraphviz Installing collected packages: pygraphviz Running setup.py install for pygraphviz ... error Complete output from command /opt/conda/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-0RJB9q/pygraphviz/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-vo59d4-record/install-record.txt --single-version-externally-managed --compile: running install Trying pkg-config Package libcgraph was not found in the pkg-config search path. Perhaps you should add the directory containing `libcgraph.pc' to the PKG_CONFIG_PATH environment variable No package 'libcgraph' found Traceback (most recent call last): File "<string>", line 1, in <module> File "/tmp/pip-build-0RJB9q/pygraphviz/setup.py", line 87, in <module> tests_require=['nose>=0.10.1', 'doctest-ignore-unicode>=0.1.0',], File "/opt/conda/lib/python2.7/distutils/core.py", line 151, in setup dist.run_commands() File "/opt/conda/lib/python2.7/distutils/dist.py", line 953, in run_commands self.run_command(cmd) File "/opt/conda/lib/python2.7/distutils/dist.py", line 972, in run_command cmd_obj.run() File "setup_commands.py", line 44, in modified_run self.include_path, self.library_path = get_graphviz_dirs() File "setup_extra.py", line 121, in get_graphviz_dirs include_dirs, library_dirs = _pkg_config() File "setup_extra.py", line 44, in _pkg_config output = S.check_output(['pkg-config', '--libs-only-L', 'libcgraph']) File "/opt/conda/lib/python2.7/subprocess.py", line 573, in check_output raise CalledProcessError(retcode, cmd, output=output) subprocess.CalledProcessError: Command '['pkg-config', '--libs-only-L', 'libcgraph']' returned non-zero exit status 1 ---------------------------------------- Command "/opt/conda/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-0RJB9q/pygraphviz/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-vo59d4-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-0RJB9q/pygraphviz/ The code that I used: import networkx as nx import matplotlib.pyplot as plt plt.switch_backend('agg') from networkx.drawing.nx_agraph import graphviz_layout import graphs A = graphs.create_graph() graph = A.graph G, labels = A.networkList() fig = plt.figure() pos = graphviz_layout(G, prog='neato') Could anyone let me know how to solve this? I highly appreciate your assistance. Thank you very much! Answer: `libcgraph` is part of the graphviz package from <http://graphviz.org/>. You'll need to install that before you install `pygraphviz`. The `graphviz` package you are installing with pip is a Python package and not the graphviz package you need.
How can I map an ontology components to a relational database? Question: I already have an owl ontology which contains classes, instances and object properties. How can I map them to a relational data base such as MYSQL using a Python as a programming language(I prefer Python) ? **For example** , an ontology can contains the classes: "Country and city" and instances like: "United states and NYC". So I need manage to store them in relational data bases' tables. I would like to know if there is some Python libraries to so. Answer: If I understand you well, I think you could use SQLite with python. SQLite is great because you have just to import the library with : import sqlite3 And then, there is no need for a server. Things are stored in a file, gerenaly ending with : `.db` Have a look at the doc, the example are helpful : <https://docs.python.org/2/library/sqlite3.html> **EDIT :** To review or create your database and tables, I advise you tu use sqlitebrowser, which is light and easy to use : <http://sqlitebrowser.org/>
Error when creating Database Tables - CKAN Question: i'm trying to install CKAN 2.5.2 on Centos 6.8 When i run paster db init -c /etc/ckan/default/development.ini i get error Traceback (most recent call last): File "/usr/lib/ckan/default/bin/paster", line 9, in <module> load_entry_point('PasteScript==2.0.2', 'console_scripts', 'paster')() File "/usr/lib/ckan/default/lib/python2.6/site-packages/paste/script/command.py", line 102, in run invoke(command, command_name, options, args[1:]) File "/usr/lib/ckan/default/lib/python2.6/site-packages/paste/script/command.py", line 141, in invoke exit_code = runner.run(args) File "/usr/lib/ckan/default/lib/python2.6/site-packages/paste/script/command.py", line 236, in run result = self.command() File "/usr/lib/ckan/default/src/ckan/ckan/lib/cli.py", line 205, in command self._load_config(cmd!='upgrade') File "/usr/lib/ckan/default/src/ckan/ckan/lib/cli.py", line 142, in _load_config conf = self._get_config() File "/usr/lib/ckan/default/src/ckan/ckan/lib/cli.py", line 139, in _get_config return appconfig('config:' + self.filename) File "/usr/lib/ckan/default/lib/python2.6/site-packages/paste/deploy/loadwsgi.py", line 261, in appconfig global_conf=global_conf) File "/usr/lib/ckan/default/lib/python2.6/site-packages/paste/deploy/loadwsgi.py", line 296, in loadcontext global_conf=global_conf) File "/usr/lib/ckan/default/lib/python2.6/site-packages/paste/deploy/loadwsgi.py", line 320, in _loadconfig return loader.get_context(object_type, name, global_conf) File "/usr/lib/ckan/default/lib/python2.6/site-packages/paste/deploy/loadwsgi.py", line 454, in get_context section) File "/usr/lib/ckan/default/lib/python2.6/site-packages/paste/deploy/loadwsgi.py", line 476, in _context_from_use object_type, name=use, global_conf=global_conf) File "/usr/lib/ckan/default/lib/python2.6/site-packages/paste/deploy/loadwsgi.py", line 406, in get_context global_conf=global_conf) File "/usr/lib/ckan/default/lib/python2.6/site-packages/paste/deploy/loadwsgi.py", line 296, in loadcontext global_conf=global_conf) File "/usr/lib/ckan/default/lib/python2.6/site-packages/paste/deploy/loadwsgi.py", line 328, in _loadegg return loader.get_context(object_type, name, global_conf) File "/usr/lib/ckan/default/lib/python2.6/site-packages/paste/deploy/loadwsgi.py", line 620, in get_context object_type, name=name) File "/usr/lib/ckan/default/lib/python2.6/site-packages/paste/deploy/loadwsgi.py", line 646, in find_egg_entry_point possible.append((entry.load(), protocol, entry.name)) File "/usr/lib/ckan/default/lib/python2.6/site-packages/pkg_resources/__init__.py", line 2229, in load return self.resolve() File "/usr/lib/ckan/default/lib/python2.6/site-packages/pkg_resources/__init__.py", line 2235, in resolve module = __import__(self.module_name, fromlist=['__name__'], level=0) File "/usr/lib/ckan/default/src/ckan/ckan/config/middleware.py", line 28, in <module> from ckan.config.environment import load_environment File "/usr/lib/ckan/default/src/ckan/ckan/config/environment.py", line 18, in <module> import ckan.lib.helpers as h File "/usr/lib/ckan/default/src/ckan/ckan/lib/helpers.py", line 30, in <module> from bleach import clean as clean_html File "/usr/lib/ckan/default/lib/python2.6/site-packages/bleach/__init__.py", line 8, in <module> from html5lib.sanitizer import HTMLSanitizer ImportError: No module named sanitizer I was following [wiki instructions](https://github.com/ckan/ckan/wiki/How-to- install-CKAN-2.5.2-on-CentOS-6.8) I'm stuck on this step and don't know how to proceed. Module html5lib is imported and updated to latest version. Paster script is executed in virtualenv under root account. Also i'm running all of this in python2.6 since that is default on Centos. Some additional information when i run python and import html5lib and then help(html5lib) i get this >>> import html5lib >>> help(html5lib) Help on package html5lib: NAME html5lib FILE /usr/lib/python2.6/site-packages/html5lib/__init__.py DESCRIPTION HTML parsing library based on the WHATWG "HTML5" specification. The parser is designed to be compatible with existing HTML found in the wild and implements well-defined error recovery that is largely compatible with modern desktop web browsers. Example usage: import html5lib f = open("my_document.html") tree = html5lib.parse(f) PACKAGE CONTENTS _ihatexml _inputstream _tokenizer _trie (package) _utils constants filters (package) html5parser serializer treeadapters (package) treebuilders (package) treewalkers (package) CLASSES __builtin__.object html5lib.html5parser.HTMLParser class HTMLParser(__builtin__.object) | HTML parser. Generates a tree structure from a stream of (possibly | malformed) HTML | | Methods defined here: : There is no sanitizer here. Do i need to use specific version of html5lib? Can anyone help? Answer: It looks like you have 2 copies of html5lib installed. When you did help(html5lib) it is showing a copy that is installed in your user's python directory (/usr/lib/python2.6/site-packages/) not the virtualenv that you have ckan (and bleach) installed (/usr/lib/ckan/default/lib/python2.6/site- packages/). So get rid of the former to avoid confusion. Yes I think you have the wrong version of html5lib, since sanitizer is listed in the package contents when I do help. This is the correct version (at the time of writing - in future check what is in requirements.txt): $ pip freeze | grep html5lib html5lib==0.9999999
execute script from within python3 interpreter and show interpreter results Question: I am working inside the python 3 interpreter, and I want to use it for homework. first, I have a library called `mylibrary.py` like this from math import pi def area_circle(r): return(r*r*pi)) Second, I have a script called `homework.py` like this from mylibrary import * area_circle(3) area_circle(5) area_circle(3) + area_circle(5) Now, I want to enter the python interpreter and somehow execute the script `homework.py` as if I had typed these lines directly into the interpreter, and I want the results to appear on the screen as the interpreter normally displays them. from the BASH prompt: $python3 Python 3.4.3 (default, Oct 14 2015, 20:28:29) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> exec "homework" Is there such a command? I want it to read my script and enter the lines into the interpreter, so that what appears on the screen next is: >>> from mylibrary import * >>> area_circle(3) 28.274333882308138 >>> area_circle(5) 78.53981633974483 >>> area_circle(3) + area_circle(5) 106.81415022205297 Please note that I do not have `Idle`, i do not have `iPython`, I just have the interpreter which I access from the BASH prompt. I know that I could replace the functions in the library to explicitly call the print function, something like this: def area_circle(r): a=r*r*pi print(a) return() But I don't want that, as it would prevent me from using this function as a building block of future functions. So I guess what I'm asking is how to execute the `homework` script line by line into the interpreter, in the simplest way possible. Answer: You can"t show the content without using 'print' if you're not using python prompt .... You can modify your function, so it prints if a second argument is given, and doesn't print otherwise : def area_circle(r, do_print=False): a=r*r*pi if do_print: print(a) return a This function will return the area in all the case. It will also print its value if you give it `True` as a second argument (`area_circle(3)` prints nothing and returns the area, while `area_circle(3,True)` print the area, and also returns its value)
read_sql_query returns far more records than actual table rows Question: I am trying to get the a postgresql table in to python data frame. But the dataframe size is extremely larger than the actual number of table records even though I specified an index column. What's causing this issue? Single quotes/ commas in the database table? import psycopg2 as pg import pandas.io.sql as psql connection = pg.connect("dbname=BeaconDB user=admin password=root") dataframe = psql.read_sql_query(sql="SELECT * from encounters2", con=connection, index_col='encounter_id') print('size::::', dataframe.size) Answer: [df.size](http://pandas.pydata.org/pandas- docs/stable/generated/pandas.DataFrame.size.html) shows you the number of elements (cells) in the DF Demo: In [134]: df Out[134]: A B 0 3 11 1 0 2 In [135]: df.size Out[135]: 4 In [136]: df.shape Out[136]: (2, 2) In [137]: len(df) Out[137]: 2 In [138]: len(df.index) Out[138]: 2
How to input data in a tensorflow learn validation monitor? Question: I'm trying to use a validation monitor in skflow by passing my validation set as numpy array. Here is some simple code to reproduce the problem (I installed tensorflow from the provided binaries for Ubuntu/Linux 64-bit, GPU enabled, Python 2.7): import numpy as np from sklearn.cross_validation import train_test_split from tensorflow.contrib import learn import tensorflow as tf import logging logging.getLogger().setLevel(logging.INFO) #Some fake data N=200 X=np.array(range(N),dtype=np.float32)/(N/10) X=X[:,np.newaxis] Y=np.sin(X.squeeze())+np.random.normal(0, 0.5, N) X_train, X_test, Y_train, Y_test = train_test_split(X, Y, train_size=0.8, test_size=0.2) val_monitor = learn.monitors.ValidationMonitor(X_test, Y_test,early_stopping_rounds=200) reg=learn.DNNRegressor(hidden_units=[10,10],activation_fn=tf.tanh,model_dir="tmp/") reg.fit(X_train,Y_train,steps=5000,monitors=[val_monitor]) print "train error:", reg.evaluate(X_train, Y_train) print "test error:", reg.evaluate(X_test, Y_test) The code runs but only the first validation step is done properly, then validation always returns the same value even if training is actually going fine which can be checked by running an evaluation on the test set at the end. The following message also appears for each validation step. INFO:tensorflow:Input iterator is exhausted. Any help is welcome! Thanks, David Answer: I was able to solve this by adding: `config=tf.contrib.learn.RunConfig(save_checkpoints_secs=1)` to the `DNNRegressor` call.
Python + OpenCV, change brightness/darkness outside a sliding window? Question: I'm working with Python and OpenCV and I'm a newbie in both. For my project, I need to move a sliding window over a picture; for each position of the window the area outside the window must be shown darker than the area inside the window. This is the part of my code that takes care of the picture and window visualization (the valid positions for the sliding window are calculated somewhere else) for (x, y, window) in valid_positions: if window.shape[0] != winH or window.shape[1] != winW: continue # Put here stuff to process the window content # i.e apply a classifier clone = image.copy() cv2.rectangle(clone, (x, y), (x + winW, y + winH), (0, 255, 0), 2) cv2.imshow("Window", clone) cv2.waitKey(1) time.sleep(0.025) The window is created and it slides on the valid positions, so that part works well. But I have absolutely no idea on how to make the picture outside the window appear darker. Any suggestions? Thanks in advance. EDIT: i forgot to add an important detail: my input images are always in black and white (not even greyscale, just black and white pixels). Maybe this makes it easier to alter the brightness/darkness? Answer: In general, you can preserve the content inside the window and lower the intensity of the entire image. Then replace the area inside the window with original content. That trick should work. This part of the code may look like clone = image.copy() windowArea = clone[y:y + winH, x:x + winW].copy() clone = np.floor(clone * 0.5).astype('uint8') # 0.5 can be adjusted clone[y:y + winH, x:x + winW] = windowArea cv2.rectangle(clone, (x, y), (x + winW, y + winH), (0, 255, 0), 2)
Python Pandas DataFrame check if string is other string and fill column Question: I am learning Python and this might be a noob question: import pandas as pd A = pd.DataFrame({"A":["house", "mouse", "car", "tree"]}) check_list = ["house", "tree"] I want to check rowwise if the string in A is in check_list. The result should be A YESorNO 0 house YES 1 mouse NO 2 car NO 3 tree YES Answer: Use [`numpy.where`](http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.where.html) with [`isin`](http://pandas.pydata.org/pandas- docs/stable/generated/pandas.Series.isin.html): import pandas as pd import numpy as np A = pd.DataFrame({"A":["house", "mouse", "car", "tree"]}) check_list = ["house", "tree"] A['YESorNO'] = np.where(A['A'].isin(check_list),'YES','NO') print (A) A YESorNO 0 house YES 1 mouse NO 2 car NO 3 tree YES
Fast calculation of v-disparity with OpenCV-Function calcHist Question: Based on a disparity matrix from a passive stereo-camera system i need to calculate a v-disparity representation for obstacle detection with OpenCV. A working implementation is **not** the problem. The problem is to do it fast... (One) Reference for v-Disparity: Labayrade, R. and Aubert, D. and Tarel, J.P. Real time obstacle detection in stereovision on non flat road geometry through v-disparity representation The basic in short, to get the v-disparity (figure [1](http://i.stack.imgur.com/fPvsg.png)), is to analyze the rows of the disparity-matrix (figure [2](http://i.stack.imgur.com/YXYvf.png)) an represent the result as a histogram for each row over the disparity values. u-disparity (figure [3](http://i.stack.imgur.com/R0qSt.png)) is the same on the columns of the disparity-matrix. (All figures are false-colored.) I have implement the "same" in Python and C++. The speed in Python is acceptable but in C++ i get for the u- and v-disparity a time round about a half second (0.5 s). _(1. edit: due to the separate time measurement, only the calculation of the u-histogram takes a big amount of time...)_ This leads me to following questions: 1. Is it possible to avoid the loops for the line-wise calculation of the histogram? Is there a "trick" to do it with one call of `calcHist`-Function from OpenCV? Perhaps with the dimensions? 2. Is it in C++ just bad-coded and the runtime-issue are not related to the loops used for calculation? Thanks, all * * * Working implementation in Python: #!/usr/bin/env python2 #-*- coding: utf-8 -*- # # THIS SOURCE-CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED. IN NO EVENT WILL THE AUTHOR BE HELD LIABLE FOR ANY DAMAGES ARISING FROM # THE USE OF THIS SOURCE-CODE. USE AT YOUR OWN RISK. import cv2 import numpy as np import time def draw_object(image, x, y, width=50, height=100): color = image[y, x] image[y-height:y, x-width//2:x+width//2] = color IMAGE_HEIGHT = 600 IMAGE_WIDTH = 800 while True: max_disp = 200 # create fake disparity image = np.zeros((IMAGE_HEIGHT, IMAGE_WIDTH), np.uint8) for c in range(IMAGE_HEIGHT)[::-1]: image[c, ...] = int(float(c) / IMAGE_HEIGHT * max_disp) draw_object(image, 275, 175) draw_object(image, 300, 200) draw_object(image, 100, 350) # calculate v-disparity vhist_vis = np.zeros((IMAGE_HEIGHT, max_disp), np.float) for i in range(IMAGE_HEIGHT): vhist_vis[i, ...] = cv2.calcHist(images=[image[i, ...]], channels=[0], mask=None, histSize=[max_disp], ranges=[0, max_disp]).flatten() / float(IMAGE_HEIGHT) vhist_vis = np.array(vhist_vis * 255, np.uint8) vblack_mask = vhist_vis < 5 vhist_vis = cv2.applyColorMap(vhist_vis, cv2.COLORMAP_JET) vhist_vis[vblack_mask] = 0 # calculate u-disparity uhist_vis = np.zeros((max_disp, IMAGE_WIDTH), np.float) for i in range(IMAGE_WIDTH): uhist_vis[..., i] = cv2.calcHist(images=[image[..., i]], channels=[0], mask=None, histSize=[max_disp], ranges=[0, max_disp]).flatten() / float(IMAGE_WIDTH) uhist_vis = np.array(uhist_vis * 255, np.uint8) ublack_mask = uhist_vis < 5 uhist_vis = cv2.applyColorMap(uhist_vis, cv2.COLORMAP_JET) uhist_vis[ublack_mask] = 0 image = cv2.applyColorMap(image, cv2.COLORMAP_JET) cv2.imshow('image', image) cv2.imshow('vhist_vis', vhist_vis) cv2.imshow('uhist_vis', uhist_vis) cv2.imwrite('disparity_image.png', image) cv2.imwrite('v-disparity.png', vhist_vis) cv2.imwrite('u-disparity.png', uhist_vis) if chr(cv2.waitKey(0)&255) == 'q': break * * * Working implementation in C++: #include <iostream> #include <stdlib.h> #include <ctime> #include <opencv2/opencv.hpp> using namespace std; void draw_object(cv::Mat image, unsigned int x, unsigned int y, unsigned int width=50, unsigned int height=100) { image(cv::Range(y-height, y), cv::Range(x-width/2, x+width/2)) = image.at<unsigned char>(y, x); } int main() { unsigned int IMAGE_HEIGHT = 600; unsigned int IMAGE_WIDTH = 800; unsigned int MAX_DISP = 250; unsigned int CYCLE = 0; //setenv("QT_GRAPHICSSYSTEM", "native", 1); // === PREPERATIONS == cv::Mat image = cv::Mat::zeros(IMAGE_HEIGHT, IMAGE_WIDTH, CV_8U); cv::Mat uhist = cv::Mat::zeros(IMAGE_HEIGHT, MAX_DISP, CV_32F); cv::Mat vhist = cv::Mat::zeros(MAX_DISP, IMAGE_WIDTH, CV_32F); cv::Mat tmpImageMat, tmpHistMat; float value_ranges[] = {(float)0, (float)MAX_DISP}; const float* hist_ranges[] = {value_ranges}; int channels[] = {0}; int histSize[] = {MAX_DISP}; struct timespec start, finish; double elapsed; while(1) { CYCLE++; // === CLEANUP == image = cv::Mat::zeros(IMAGE_HEIGHT, IMAGE_WIDTH, CV_8U); uhist = cv::Mat::zeros(IMAGE_HEIGHT, MAX_DISP, CV_32F); vhist = cv::Mat::zeros(MAX_DISP, IMAGE_WIDTH, CV_32F); // === CREATE FAKE DISPARITY WITH OBJECTS === for(int i = 0; i < IMAGE_HEIGHT; i++) image.row(i) = ((float)i / IMAGE_HEIGHT * MAX_DISP); draw_object(image, 200, 500); draw_object(image, 525 + CYCLE%100, 275); draw_object(image, 500, 300 + CYCLE%100); clock_gettime(CLOCK_MONOTONIC, &start); // === CALCULATE V-HIST === for(int i = 0; i < IMAGE_HEIGHT; i++) { tmpImageMat = image.row(i); vhist.row(i).copyTo(tmpHistMat); cv::calcHist(&tmpImageMat, 1, channels, cv::Mat(), tmpHistMat, 1, histSize, hist_ranges, true, false); vhist.row(i) = tmpHistMat.t() / (float) IMAGE_HEIGHT; } clock_gettime(CLOCK_MONOTONIC, &finish); elapsed = (finish.tv_sec - start.tv_sec); elapsed += (finish.tv_nsec - start.tv_nsec) * 1e-9; cout << "V-HIST-TIME: " << elapsed << endl; clock_gettime(CLOCK_MONOTONIC, &start); // === CALCULATE U-HIST === for(int i = 0; i < IMAGE_WIDTH; i++) { tmpImageMat = image.col(i); uhist.col(i).copyTo(tmpHistMat); cv::calcHist(&tmpImageMat, 1, channels, cv::Mat(), tmpHistMat, 1, histSize, hist_ranges, true, false); uhist.col(i) = tmpHistMat / (float) IMAGE_WIDTH; } clock_gettime(CLOCK_MONOTONIC, &finish); elapsed = (finish.tv_sec - start.tv_sec); elapsed += (finish.tv_nsec - start.tv_nsec) * 1e-9; cout << "U-HIST-TIME: " << elapsed << endl; // === PREPARE AND SHOW RESULTS === uhist.convertTo(uhist, CV_8U, 255); cv::applyColorMap(uhist, uhist, cv::COLORMAP_JET); vhist.convertTo(vhist, CV_8U, 255); cv::applyColorMap(vhist, vhist, cv::COLORMAP_JET); cv::imshow("image", image); cv::imshow("uhist", uhist); cv::imshow("vhist", vhist); if ((cv::waitKey(1)&255) == 'q') break; } return 0; } * * * [![enter image description here](http://i.stack.imgur.com/fPvsg.png)](http://i.stack.imgur.com/fPvsg.png) Figure 1: v-disparity [![fake disparity matrix](http://i.stack.imgur.com/YXYvf.png)](http://i.stack.imgur.com/YXYvf.png) Figure 2: Fake disparity matrix [![enter image description here](http://i.stack.imgur.com/R0qSt.png)](http://i.stack.imgur.com/R0qSt.png) Figure 3: u-disparity * * * 1. edit: * correct name for u- and v-disparity and separate time measurement in c++ example * small typo Answer: Today i had the possibility to reinvestigate the problem. Remembering the OpenCV basics ([1](http://docs.opencv.org/2.4/doc/tutorials/core/how_to_scan_images/how_to_scan_images.html)) for the `Mat`-structure and the fact that only one calculation takes a huge amount of time, i had the solution. In OpenCV, each row of an image could be reached by a row-pointer. For iterating columns (done in u-disparity calculation) i suspect, that OpenCV needs to resolve every row-pointer + column-offset for building the histogram. Changing the Code in a way, that OpenCV is able to use row-pointer, solves the problem for me. | old code [s] | changed [s] ------------+--------------+------------- V-HIST-TIME | 0.00351909 | 0.00334152 U-HIST-TIME | 0.600039 | 0.00449285 So for the u-hist-loop i transpose the image and reverse the operation after the loop. The line wise access for calculation could now be done via the row- pointer. Changed Codelines: // === CALCULATE U-HIST === image = image.t(); for(int i = 0; i < IMAGE_WIDTH; i++) { tmpImageMat = image.row(i); uhist.col(i).copyTo(tmpHistMat); cv::calcHist(&tmpImageMat, 1, channels, cv::Mat(), tmpHistMat, 1, histSize, hist_ranges, true, false); uhist.col(i) = tmpHistMat / (float) IMAGE_WIDTH; } image = image.t(); * * * Finally my second question takes effect, the runtime-issue belongs not to the loop. A time less than 5 ms is (for now) fast enough.
Python: sorting list of lists not functioning as intended Question: I am trying to sort the following list of lists by the first item of each list in ascending order: framenos = [ ['1468', '2877', 'Pos.:', 95], ['3185', '4339', 'Pos.:', 96], ['195', '1460', 'Pos.:', 97] ] I am using the following to do so: framesorted = sorted(framenos, key=lambda x: x[0]) #sort ranges by start numbers Which gives: [['1468', '2877', 'Pos.:', 95], ['195', '1460', 'Pos.:', 97], ['3185', '4339', 'Pos.:', 96]] What's going wrong? Answer: Your values are strings, so you are sorting [_lexicographically_](https://en.wikipedia.org/wiki/Lexicographical_order), not numerically. `'1468'` is sorted before `'195'` because `'4'` comes before `'9'` in the ASCII standard, just like `'Ask'` would be sorted before `'Attribution'`. Convert your strings to numbers if you need a numeric sort: framesorted = sorted(framenos, key=lambda x: int(x[0])) Demo: >>> framenos = [ ... ['1468', '2877', 'Pos.:', 95], ... ['3185', '4339', 'Pos.:', 96], ... ['195', '1460', 'Pos.:', 97] ... ] >>> sorted(framenos, key=lambda x: int(x[0])) [['195', '1460', 'Pos.:', 97], ['1468', '2877', 'Pos.:', 95], ['3185', '4339', 'Pos.:', 96]] >>> from pprint import pprint >>> pprint(_) [['195', '1460', 'Pos.:', 97], ['1468', '2877', 'Pos.:', 95], ['3185', '4339', 'Pos.:', 96]]
How to remove double quotes from index of csv file in python Question: I'm trying to read multiple `csv` files with python. The index of raw data(or the first column) has a little problem, the partial csv file looks like this: NoDemande;"NoUsager";"Sens";"IdVehiculeUtilise";"NoConducteur";"NoAdresse";"Fait";"HeurePrevue" 42210000003;"42210000529";"+";"265Véh";"42210000032";"42210002932";"1";"25/07/2015 10:00:04" 42210000005;"42210001805";"+";"265Véh";"42210000032";"42210002932";"1";"25/07/2015 10:00:04" 42210000004;"42210002678";"+";"265Véh";"42210000032";"42210002932";"1";"25/07/2015 10:00:04" 42210000003;"42210000529";"—";"265Véh";"42210000032";"42210004900";"1";"25/07/2015 10:50:03" 42210000004;"42210002678";"—";"265Véh";"42210000032";"42210007072";"1";"25/07/2015 11:25:03" 42210000005;"42210001805";"—";"265Véh";"42210000032";"42210004236";"1";"25/07/2015 11:40:03" The first index has no `""`, after reading the file, it looks like: `"NoDemande"` while others have no `""`, and the rest of column looks just fine, which makes the result looks like(not the same lines): "NoDemande" NoUsager Sens IdVehiculeUtilise NoConducteur NoAdresse Fait HeurePrevue 42209000003 42209001975 + 245Véh 42209000002 42209005712 1 24/07/2015 06:30:04 42209000004 42209002021 + 245Véh 42209000002 42209005712 1 24/07/2015 06:30:04 42209000005 42209002208 + 245Véh 42209000002 42209005713 1 24/07/2015 06:45:04 42216000357 42216001501 - 190Véh 42216000139 42216001418 1 31/07/2015 17:15:03 42216000139 42216000788 - 309V7pVéh 42216000059 42216006210 1 31/07/2015 17:15:03 42216000118 42216000188 - 198Véh 42216000051 42216006374 1 31/07/2015 17:15:03 It causes problem identifying name of index in the coming moves. How to solve this problem? Here's my code of reading files: import pandas as pd import glob pd.set_option('expand_frame_repr', False) path = r'D:\Python27\mypfe\data_test' allFiles = glob.glob(path + "/*.csv") frame = pd.DataFrame() list_ = [] for file_ in allFiles: #Read file df = pd.read_csv(file_,header=0,sep=';',dayfirst=True,encoding='utf8', dtype='str') df['Sens'].replace(u'\u2014','-',inplace=True) list_.append(df) print"fichier lu ",file_ frame = pd.concat(list_) print frame Answer: In fact, I was stuck with how to remove double quotes from index. After changing the angle, I think maybe it's better to add a new column, copying the values from the original one and delete it. So the new column will have the index you want. In my case, I did: frame['NoDemande'] = frame.ix[:, 0] tl = frame.drop(frame.columns[0],axis=1) So I got a new one with all I wanted.
How to make a GET call by executing a javascript file and print response in terminal Question: So I am using python for all needs in my life and have never touched javascript. Now I might need to learn javascript because of certain reasons. First thing that came to my mind is to try to make a GET call and print the response body. I seem to have trouble finding the answer for this questions. I want to have a javascript file called `get.js`. Then I want to run the file by `node get.js` and print the response body to terminal. I basically want this translated from python to javascript: Python script: import requests r = requests.get("http://ip.jsontest.com/") print r.text In terminal: $ python get.py {"ip": "80.147.208.248"} Answer: So this seems to be the way. I needed to search for "node get request" not "javascript get request". var http = require('http'); var options = { host: 'ip.jsontest.com', path: '/' }; callback = function(response) { var str = ''; response.on('data', function (chunk) { str += chunk; }); response.on('end', function () { console.log(str); }); } http.request(options, callback).end();
recursing a list of 73033 elements with HTML tags and get context from it Question: I have a long list of elements with a length of 73,033. I would like to get the context from it. In the list, each element has the same structure (if the block of the following code helps), and it looks like this `<div align="center" class="photocaption"> Author/Designer Carleton Varney with Jim Druckman </div>`. What I am interested in getting is the text `Author/Designer Carleton Varney with Jim Druckman.` **Main code** NewSoups = [BeautifulSoup(NewR) for NewR in NewRs]. captions = [soup.find_all("div", class_ = "photocaption") for soup in NewSoups] flattened_captions = [] for x in captions: for y in x: flattened_captions.append(y) print(len(flattened_captions)) #73033 import re results = [re.sub('<[^>]*>', '', y) for y in flattened_captions] #where the error comes from **Error** Traceback (most recent call last): File "picked.py", line 22, in <module> results = [re.sub('<[^>]*>', '', y) for y in flattened_captions] File "/opt/conda/lib/python2.7/re.py", line 155, in sub return _compile(pattern, flags).sub(repl, string, count) TypeError: expected string or buffer I am wondering if there is a convenient way to loop through the long list of `<div ></div>`. If not, what would be the best way to extract all text that I desire? Thank you very much. Answer: What I am going to post is not the most elegant or efficient way to deal with the posted problem. As Welbog pointed out, BeautifulSoup itself has provided the function of extracting context. As I received the error as I posted my original question, however, I was just curious where the error came from. It turned out that the returned things from flattented_captions were not strings. It's quite simple to solve. Method as below. str_flattened_captions = [str(flattened_captions[i]) for i in range(len(flattened_captions))] gains = [re.sub('<[^>]*>', '', item) for item in str_flattened_captions] To test print(gains[:5]) r Barbara Schorr ', ' Architect Joan Dineen with Alyson Liss ', ' Author/Designer Carleton Varney with Jim Druckman ', ' Designers Richard Cerrone, Lisa Hyman and Rhonda Eleish (front) in their room called "Holiday Nod To Nature" ']
Python 2.7: Returning a value in a csv file from input Question: I've got a csv with: T,8,101 T,10,102 T,5,103 and need to search the csv file, in the 3rd column for my input, and if found, return the 2nd column value in that same row (searching "102" would return "10"). I then need to save the result to use in another calculation. (I am just trying to print the result for now..) I am new to python (2 weeks) and wanted to get a grasp on reading/writing in csv files. All the searchable results, didn't give me the answer I needed. Thanks Here is my code: name = input("waiting") import csv with open('cards.csv', 'rt') as csvfile: reader = csv.reader(csvfile, delimiter=',') for row in reader: if row[2] == name: print(row[1]) Answer: As stated in my comment above I would implement a general approach without using the csv-module like this: import io s = """T,8,101 T,10,102 T,5,103""" # use io.StringIO to get a file-like object f = io.StringIO(s) lines = [tuple(line.split(',')) for line in f.read().splitlines()] example_look_up = '101' def find_element(look_up): for t in lines: if t[2] == look_up: return t[1] result = find_element(example_look_up) print(result) Please keep in mind, that this is Python3-code. You need to replace `print()` with `print` if using with Python2 and maybe change something related to the `StringIO` which I am using for demonstration purposes here in order to get a file-like object. However, this snippet should give you a basic idea about a possible solution.
Django: how to cache a function Question: I have a web application that runs python in the back-end. When my page loads, a django function is called which runs a SQL query and that query takes about 15-20 seconds to run and return the response. And that happens every time the page loads and it would be very annoying for the user to wait 15-20 secs every time the page refreshes. So I wanted to know if there is a way to cache the response from the query and store it somewhere in the browser when the page loads the first time. And whenever, the page refreshes afterwards, instead of running the query again, I would just get the data from browser's cache and so the page would load quicker. This is the function that runs when the page loads def populateDropdown(request): database = cx_Oracle.connect('username', 'password', 'host') cur = database.cursor() cur.execute("select distinct(item) from MY_TABLE") dropList = list(cur) dropList = simplejson.dumps({"dropList": dropList}) return HttpResponse(dropList, content_type="application/json") I can't seem to find an example on how to do this. I looked up Django's documentation on caching but it shows how to cache entire page not a specific function. It would be great if you can provide a simple example or link to a tutorial. Thanks :) Answer: You can the cache the result of the view that runs that query: from django.views.decorators.cache import cache_page @cache_page(600) # 10 minutes def populateDropdown(request): ... Or cache the expensive functions in the view which in your case is almost synonymous to caching the entire view: from django.core.cache import cache def populateDropdown(request): if not cache.get('droplist'): # check if droplist has expired in cache database = cx_Oracle.connect('username', 'password', 'host') cur = database.cursor() cur.execute("select distinct(item) from MY_TABLE") dropList = list(cur) dropList = simplejson.dumps({"dropList": dropList}) cache.set('droplist', dropList, 600) # 10 minutes return HttpResponse(cache.get('droplist'), content_type="application/json")
beautifulsoup with get_text - handle spaces Question: I using BS4 (python3) for extracting text from html file. My file looks like this: <BODY> <P>Hello World!</P> </BODY> </HTML> When i calling `get_text()` method, the output is "Hello World!". Because it's HTML, I was expected to get "Hello World!" (two or more spaces are replaced with one space in HTML). This Also relevant for this situation: <BODY> <P>Hello World!</P> </BODY> </HTML> I was expected to find "Hello World!" but it was "Hello \n World!". How I can achieve my goal? Answer: The problem is, neither `get_text(strip=True)` nor joining the `.stripped_strings` would work here because there is a single `NavigableString` in the `p` element in the second case and it's value is `Hello\n World!`. The newline is inside the text node in other words. In this case, you will have to _replace the newlines_ manually: soup.p.get_text().replace("\n", "") * * * Or, to also handle the `br` elements (replacing them with newlines), you can make a converting function that will prepare the text for you: from bs4 import BeautifulSoup, NavigableString data = """ <BODY> <P>Hello World!</P> <P>Hello <BR/> World!</P> </BODY> </HTML> """ def replace_with_newlines(element): text = '' for elem in element.children: if isinstance(elem, NavigableString): text += elem.replace("\n", "").strip() elif elem.name == 'br': text += '\n' return text soup = BeautifulSoup(data, "html.parser") for p in soup.find_all("p"): print(replace_with_newlines(p)) Prints (no newlines in the first case, a single newline in the second): Hello World! Hello World!
Error loading a image file into ndarray with scipy.ndimage.imread Question: Im trying to load a image file into a ndarray with something like this: image_data = ndimage.imread(image_file).astype(float) but i get this error: /home/milos/anaconda3/envs/tensorflow/lib/python3.5/site-packages/scipy/ndimage/io.py in imread(fname, flatten, mode) 23 if _have_pil: 24 return _imread(fname, flatten, mode) ---> 25 raise ImportError("Could not import the Python Imaging Library (PIL)" 26 " required to load image files. Please refer to" 27 " http://pypi.python.org/pypi/PIL/ for installation" ImportError: Could not import the Python Imaging Library (PIL) required to load image files. Please refer to http://pypi.python.org/pypi/PIL/ for installation instructions. I have Pillow installed inside the environment from which im running the notebook, it also shows up on pip freeze. I also tried running it from the console but got similar error. Any ideas how to fix this? Or is there a alternative way to load a image into ndarray? Answer: Managed to do it in the end by bypassing scipy : from PIL import Image img = Image.open(image_file) image_data = np.array(img).astype(float) would still like to know what the problem is with scipy, so please post if you know it Edit : Found a better solution: import matplotlib.pyplot as plt import matplotlib.image as mpimg image_data = mpimg.imread(image_file) this creates a numpy ndarray and normalizes the pixel depths to 0-1, and it worked nicely if i wanted to do a backwards conversion to check if its still good: plt.imshow(image_data)
Difference between a list & a stack in python? Question: What is the difference between a list & a stack in python? I have read its explanation in the python documentation but there both the things seems to be same? >>> stack = [3, 4, 5] >>> stack.append(6) >>> stack.append(7) >>> stack [3, 4, 5, 6, 7] >>> stack.pop() 7 >>> stack [3, 4, 5, 6] >>> stack.pop() 6 >>> stack.pop() 5 >>> stack [3, 4] Answer: A stack is a _data structure concept_. The documentation uses a Python `list` object to implement one. That's why that section of the tutorial is named _Using Lists as Stacks_. Stacks are just things you add stuff to, and when you take stuff away from a stack again, you do so in reverse order, first in, last out style. Like a stack of books or hats or... _beer crates_ : [![beer crate stacking](http://i.stack.imgur.com/9jJBd.jpg)](https://www.youtube.com/watch?v=9SReWtHt68A) See the [Wikipedia explanation](https://en.wikipedia.org/wiki/Stack_\(abstract_data_type\)). Lists on the other hand are far more versatile, you can add and remove elements anywhere in the list. You wouldn't try that with a stack of beer crates with someone on top! You could implement a stack with a custom class: from collections import namedtuple class _Entry(namedtuple('_Entry', 'value next')): def _repr_assist(self, postfix): r = repr(self.value) + postfix if self.next is not None: return self.next._repr_assist(', ' + r) return r class Stack(object): def __init__(self): self.top = None def push(self, value): self.top = _Entry(value, self.top) def pop(self): if self.top is None: raise ValueError("Can't pop from an empty stack") res, self.top = self.top.value, self.top.next return res def __repr__(self): if self.top is None: return '[]' return '[' + self.top._repr_assist(']') Hardly a list in sight (somewhat artificially), but it is definitely a stack: >>> stack = Stack() >>> stack.push(3) >>> stack.push(4) >>> stack.push(5) >>> stack [3, 4, 5] >>> stack.pop() 5 >>> stack.push(6) >>> stack [3, 4, 6] >>> stack.pop() 6 >>> stack.pop() 4 >>> stack.pop() 3 >>> stack [] The Python standard library doesn't come with a specific stack datatype; a `list` object does just fine. Just limit any use to `list.append()` and `list.pop()` (the latter with no arguments) to treat a list _as_ a stack.
How to recognise urls starting with anchor(#) in urls.py file in django? Question: I have starting building my application in angularJS and django, and after creating a login page, I am trying to redirect my application to a new url after successful login. I am using `$location` variable to redirect my page. Here is my code: $scope.login = function() { $http({ method: 'POST', data: { username: $scope.username, password: $scope.password }, url: '/pos/login_authentication/' }).then(function successCallback(response) { user = response.data console.log(response.data) if (user.is_active) { $location.url("dashboard") } }, function errorCallback(response) { console.log('errorCallback') }); } My initial url was `http://localhost:8000/pos/`, and after hitting the log in button, the above function calls, and I am redirected to `http://localhost:8000/pos/#/dashboard`. But I am unable to catch this url in my regex pattern in `urls.py` file: My project `urls.py` file: from django.conf.urls import url, include from django.contrib import admin urlpatterns = [ url(r'^pos/', include('pos.urls')), url(r'^admin/', admin.site.urls), ] And my `pos` application's `urls.py` file: urlpatterns = [ url(r'^$', views.index, name='index'), url(r'^login_authentication/$', views.login_authentication, name='login_authentication'), url(r'^#/dashboard/$', views.dashboard, name='dashboard') ] Using this, I am getting the same login page on visiting this `http://localhost:8000/pos/#/dashboard` link. This means that in my `urls.py` file of my `pos` application, it is mapping my `http://localhost:8000/pos/#/dashboard` to first object of `urlpatterns`:`url(r'^$', views.index, name='index')`. How do I make python differentiate between both the links? Answer: You have some major misunderstanding about and anchor in url. The anchor is called officially [`Fragment identifier`](https://en.wikipedia.org/wiki/Fragment_identifier), it's not part of the main url, so if you have `#` when you visit an url like `http://localhost:8000/pos/#/dashboard`, your browser would treat the remaining `#/dashboard` as the anchor in page that `http://localhost:8000/pos/` renders. You shouldn't be even using it in your `urls.py` definition. Please read the link above more carefully about the usage of an anchor.
What does ./!$ mean in Linux? Question: A noobie Linux learner here. I created a python script and `chmod 700 filename.py`, when I was going to using `./filename.py`, my instructor came and use `./!$` to run the file. What does the `./!$` that actually mean? I couldn't google it out. I'd greatly appreciate for a link of cheatsheet for the similar commend too. Thanks in advance. Answer: Suppose I just ran a command `python test.py`. This was my last command I entered into the shell. However, its argument was `test.py`. Remembering that `./` refers to the current working directory, when I type `./!$` I get the following output: $ ./!$ ./test.py ./test.py: line 1: import: command not found ./test.py: line 2: $'\r': command not found ./test.py: line 3: syntax error near unexpected token `(' '/test.py: line 3: `df = pd.DataFrame([ By context clues my last argument was used as the `!$`. If I enter several arguments such as `python test.py test2.py` I get: $ ./!$ ./test2.py ./test2.py: line 1: import: command not found Unable to initialize device PRN Confirming my intuition.
Outer addition and subtraction in tensorflow Question: Is the an equivalent operation (or series of operations) that acts like the numpy outer functions? import numpy as np a = np.arange(3) b = np.arange(5) print np.subtract.outer(a,b) [[ 0 -1 -2 -3 -4] [ 1 0 -1 -2 -3] [ 2 1 0 -1 -2]] The obvious candidate [`tf.sub`](https://www.tensorflow.org/versions/r0.9/api_docs/python/math_ops.html#sub) seems to only act elementwise. Answer: Use broadcasting: sess.run(tf.transpose([tf.range(3)]) - tf.range(5)) Output array([[ 0, -1, -2, -3, -4], [ 1, 0, -1, -2, -3], [ 2, 1, 0, -1, -2]], dtype=int32) To be more specific, given `(3, 1)` and `(1, 5)` arrays, broadcasting is mathematically equivalent to tiling the arrays into matching `(3, 5)` shapes and doing operation pointwise [![enter image description here](http://i.stack.imgur.com/nHVSb.png)](http://i.stack.imgur.com/nHVSb.png) This tiling is internally implemented by looping over existing data, so no extra memory is needed. When given unequal ranks with shapes like `(3, 1)` and `(5)`, broadcasting will pad smaller shape with`1's` _on the left_. This means that 1D list like `tf.range(5)` is treated as a row-vector, and equivalent to `[tf.range(5)]`
How to create pandas dataframe from python dictionary? Question: I have a python dictionary below: dict = {'stock1': (5,6,7), 'stock2': (1,2,3),'stock3': (7,8,9)}; I want to change dictionary to dataframe like: [![enter image description here](http://i.stack.imgur.com/puqs2.png)](http://i.stack.imgur.com/puqs2.png) How to write the code? I use: pd.DataFrame(list(dict.iteritems()),columns=['name','closePrice']) But it will get wrong. Could anyone help? Answer: You are overcomplicating the problem, just pass your dictionary into the `DataFrame` constructor: import pandas as pd d = {'stock1': (5,6,7), 'stock2': (1,2,3),'stock3': (7,8,9)} print(pd.DataFrame(d)) Prints: stock1 stock2 stock3 0 5 1 7 1 6 2 8 2 7 3 9
Date field in SAS imported in Python pandas Dataframe Question: I have imported a SAS dataset in python dataframe using Pandas `read_sas(path)` function. REPORT_MONTH is a column in sas dataset defined and saved as DATE9. format. This field is imported as float64 datatype in dataframe and having numbers which is basically a sas internal numbers for storing a date in a sas dataset. Now wondering how can I convert this originally a date field into a date field in dataframe? Answer: I don't know how python stores dates, but SAS stores dates as numbers, counting the number of days from Jan 1, 1960. Using that you should be able to convert it in python to a date variable somehow. I'm fairly certain that when data is imported to python the formats aren't honoured so in this case it's easy to work around this, in others it may not be. There's probably some sort of function in python to create a date of Jan 1, 1960 and then increment by the number of days you get from the imported dataset to get the correct date.
VK API : automatically generate acess token by using python Question: I am trying to acess VK Api with scope of stats. manually i am able to generate acess token ,but that is going to be expire in 24 hours. So i wanted to generate acess token programatcally. i tried in 2 ways mentioned as below. first way: import requests response=requests.get('https://oauth.vk.com/authorize?client_id=myclientid&scope=stats&redirect_uri= myredirecturi& display=page&v=5.53&response_type=token ') print r.url (my expectation is it should give that redirect url with acess token, but that is returning url which i am passing as argument.) second method: import vk session=vk.AuthSession(app_id='myappid', user_login='myusername', user_password='mypassword') api = vk.API(session) returning Vkauth error 'redirect_uri' mismatch. Can anyone suggest how to solve this and how to get acess token automatically. Answer: Look at the way of authorizing I described in [that](http://stackoverflow.com/questions/31912670/spring-social-vk-vkontakte- how-to-fetch-user-photo-albums/31915379#31915379) post. It's called 'straight auth' which is used by official VK applications for iOS/Android/etc, that token has grant rights and no limit for its lifetime value.
wxPython produces no GUI output Question: Am new to GUI with wxPython.Was trying out this block of code from a book and it produces the following output but no GUI with the message string.Here's the code.. [Here's the code ](http://i.stack.imgur.com/RP2Ox.png) [And Here's the output](http://i.stack.imgur.com/vJSIE.png) Answer: Python is case-sensitive and you need to use an uppercase 'O' in `OnInit`. import wx class MyApp(wx.App): def OnInit(self): wx.MessageBox('Hello Brian', 'wxApp') return True app = MyApp() app.MainLoop()
How exactly does Python resolve "from X import Y" if there are multiple Ys in X? Question: I have a package X which contains two different things named Y One is a module: # X/Y.py print 'hello' The other is a variable: # X/__init__.py Y = 'world' If I execute `from X import Y` which Y do I get and _why_? What determines the order and shadowing rules for import statements? Lastly, is there anything I might do accidentally that would change the answer? Basically I got a bug report that indicates on a user's machine this code results in the opposite Y importing from what I get on my machine. I don't have access to the user's machine, so I am trying to figure out what happened. I am wondering if there are clues in this previous question: [python: from x import y changes previous import result](http://stackoverflow.com/questions/37317153/python-from-x-import-y- changes-previous-import-result) Answer: Modules in packages, once imported, are also set as an attribute on the parent module object. The `from module import name` syntax, however, will _first_ look at the attributes of the imported module object (the globals of `X`) to resolve `name`. So, the answer is that it depends. If you have not imported the `X.Y` module yet, then you'll end up with `Y` bound to `'world'`. If you have imported `X.Y` (with `import X.Y` or `from X.Y import some_name`), then `Y` is bound to the `X.Y` submodule. The latter is a side-effect of `Y` in `X` having been set as a global. Demo: $ mkdir foo $ cat << EOF > foo/__init__.py > bar = 'from the foo package' > EOF $ cat << EOF > foo/bar.py > baz = 'from the foo.bar module' > EOF $ bin/python -c 'from foo import bar; print(bar)' from the foo package $ bin/python -c 'import foo.bar; from foo import bar; print(bar); print(bar.baz)' <module 'foo.bar' from 'foo/bar.py'> from the foo.bar module
how to detect spaces, special characters in html tags in python Question: For the following input I/O 1< img > '< input > I/O 1<' img > '< input > I want the required output as below and this should happen if `<` followed by space is present. I/O 1<img>'<input> Can anyone help me with regular expression? Answer: Try `<\s+`, `\s+>`, and `>\s+`: import re s = "I/O 1< img > '< input >" s = re.sub(r"<\s+", "<", s) s = re.sub(r"\s+>", ">", s) s = re.sub(r">\s+", ">", s) print(s) Output: I/O 1<img>'<input>
Submitting a Form using Selenium in Python Question: I need to scrape some data behind those hyperlinks from [this Site](http://www.echemportal.org/echemportal/propertysearch/treeselect_input.action?queryID=PROQ3h3n). However, those hyperlinks are `javascript function calls`, which later submits a `form` using `post` method. After some search, `selenium` seems to be a candidate. So my question is that how should I properly set a value to an input tag and submit the form which does not a submit a button. from selenium import webdriver url = "http://www.echemportal.org/echemportal/propertysearch/treeselect_input.action?queryID=PROQ3h3n" driver = webdriver.Firefox() driver.get(url) treePath_tag = driver.find_element_by_name("treePath") Before submitting the form, I need to assign value to tag `<input>`. However, I got an error > Message: Element is not currently visible and so may not be interacted with treePath_tag.send_keys('/TR.SE00.00/QU.SE.DATA_ENV/QU.SE.ENV_ENVIRONMENT_DATA/QU.SE.EN_MONITORING') IF above is correct, I would like to submit form this way. Is it correct? selenium.find_element_by_name("add_form").submit() Below are sources from the web page. ### JavaScript function <script type="text/javascript"> function AddBlock(path){ document.add_form.treePath.value=path; document.add_form.submit(); } </script> ### form "add_form" <form id="addblock_input" name="add_form" action="/echemportal/propertysearch/addblock_input.action" method="post" style="display:none;"> <table class="wwFormTable" style="display:none;"><tr style="display:none;"> <td colspan="2"> <input type="hidden" name="queryID" value="PROQ3h1w" id="addblock_input_queryID"/> </td> </tr> <tr style="display:none;"> <td colspan="2"> <input type="hidden" name="treePath" value="" id="addblock_input_treePath"/> </td> </tr> </table></form> ### div with javascript call <div id="querytree"> <h1>Property Search</h1> <h2>Select Query Block Type</h2> <p>Select a section for which to define query criteria.</p> <div class="queryblocktools"><a href="javascript:document.load_form.submit();"><img style="vertical-align:top;" alt="Load" src="/echemportal/etc/img/load.gif"/>&nbsp;Load Query</a></div> <ul class="listexpander"> <li>Physical and chemical properties<ul> <li><a href="javascript:AddBlock('/TR.SE00.00/QU.SE.DATA_PHYS/QU.SE.PC_MELTING');">Melting point/freezing point</a></li> <li><a href="javascript:AddBlock('/TR.SE00.00/QU.SE.DATA_PHYS/QU.SE.PC_BOILING');">Boiling point</a></li> </ul> </div> Answer: You are trying to set value on `hidden` input is which not visible on the page, that's why error has occurred. If you want to set value on `hidden` field try using `execute_script` as below :- treePath_tag = driver.find_element_by_name("treePath") driver.execute_script('arguments[0].value = arguments[1]', treePath_tag, '/TR.SE00.00/QU.SE.DATA_ENV/QU.SE.ENV_ENVIRONMENT_DATA/QU.SE.EN_MONITORING') After setting value on `hidden` field you can use following to `submit` the form :- selenium.find_element_by_name("add_form").submit() Hope it helps..:)
Change array shape of an image in python Question: When I read a colour image in OpenCV, it is showing the dimensions as 256x256x3. But I need to pass it as 3x256x256 array to my neural network. How do I change the array shape, retaining the pixel locations in BGR. Answer: You can simply transpose the array. For an example, my picture is a 10 x 10 picture: import numpy as np #my picture wrong_format = np.arange(300).reshape(10,10,3) correct_format = wrong_format.T If it works properly, then correct_format(0,1,1) should be equal to wrong_format(1,1,0). And we can see that it is: correct_format(0,1,1) == wrong_format(1,1,0) True
xml.etree.ElementTree.ParseError: not well-formed (invalid token) in Python Question: I am trying to open an XML file using `ElementTree` but an error occurred: > xml.etree.ElementTree.ParseError: not well-formed (invalid token) And here is my code: # -*- coding: utf-8 -*- import xml.etree.ElementTree as etree def main(): tree = etree.parse('test.xml') print 'parsing Success!' if __name__ == "__main__": main() How can I fix this error? Answer: The [XML format rules](http://www.w3schools.com/xml/xml_syntax.asp) state that you have to have one root element. Your document has two of them, `pdml` and `packet`. I'm not familiar with PDML, but a XML parser probably will choke on that.
Python Pandas v0.18+: is there a way to resample a dataframe without filling NAs? Question: I wonder if there is a way to up resample a `DataFrame` without having to decide how NAs should be filled immediately. I tried the following but got the Future Warning: > FutureWarning: .resample() is now a deferred operation use > .resample(...).mean() instead of .resample(...) Code: import pandas as pd dates = pd.date_range('2015-01-01', '2016-01-01', freq='BM') dummy = [i for i in range(len(dates))] df = pd.DataFrame({'A': dummy}) df.index = dates df.resample('B') **Is there a better way to do this, that doesn't show warnings?** Thanks. Answer: Use [`Resampler.asfreq`](http://pandas.pydata.org/pandas- docs/stable/generated/pandas.tseries.resample.Resampler.asfreq.html): print (df.resample('B').asfreq()) A 2015-01-30 0.0 2015-02-02 NaN 2015-02-03 NaN 2015-02-04 NaN 2015-02-05 NaN 2015-02-06 NaN 2015-02-09 NaN 2015-02-10 NaN 2015-02-11 NaN 2015-02-12 NaN 2015-02-13 NaN 2015-02-16 NaN 2015-02-17 NaN 2015-02-18 NaN 2015-02-19 NaN 2015-02-20 NaN 2015-02-23 NaN 2015-02-24 NaN 2015-02-25 NaN 2015-02-26 NaN 2015-02-27 1.0 2015-03-02 NaN 2015-03-03 NaN 2015-03-04 NaN ... ...
write() argument must be str, not bytes Question: I'm a beginner programmer and am working through the book python for the absolute beginner. I have come across a problem trying to write a high scoring function for the trivia game. when the function 'highscore(user, highscore):' is called on I try to assign the arguments accordingly so I can pickle the information to a file for later use. however I am running into an error trying to dump the info needed. def highscore(user, highscore): '''stores the players score to a file.''' import pickle, shelve user = '' highscore = 0 #Hscore = shelve.open('highscore.dat', 'c') Hscore = open('highscore.txt', 'a') pickle.dump(user, Hscore) pickle.dump(highscore, Hscore) #Hscore.sync() Hscore.close() since I'm working through the book and have also seen shelves in action I tried using them too but run into their own set of errors. so ignore the '#'s at this time. at the part pickle.dump is where I'm generating an error. I keep getting (as the title suggests) a write argument error. I don't understand why its not recognizing them as string. as when they are defined in the main function it is indeed a string.. Answer: Looks like you are working through a book aimed at Python 2. You need to open your file in _binary mode_ ; add `b` to the mode: Hscore = open('highscore.txt', 'ab') If your book contains more issues like these, it may be time to switch to one that supports Python 3 or to install Python 2.7 at least for the purposes of completing the book exercises.
How do I declare Global Variables correctly in Python 3.3 so they can work in multiple functions? Question: Right now I'm trying to make a little game for my friends for fun in **Python 3.3**. It was all going well until I forgot how to get certain variables (in my case gold, damage and XP) to carry over through different functions (Menu, Hills). If someone could help me fix this code so the three aforementioned variables can carry over unchanged between my functions, that would b amazing. Here's my code. import os import random import time def Menu(): global damage global xp global gold damage = 1 xp = 0 gold = 0 print("\nyour attack value is", damage,"\nyour xp level is", xp,"\nand you have", gold,"gold.\n") sword = input("Old Joe Smith: Do you need a sword? ") sword = sword.lower() if sword == "yes": damage = damage + 9 print("Old Joe Smith: *Gives you a sharp steel sword* Good luck on your travels!\n\nYour new attack value is",damage,"\n") time.sleep(2) Hills() if sword == "yeah": damage = damage + 9 print("Old Joe Smith: *Gives you a sharp steel sword* Good luck on your travels!\n\nYour new attack value is",damage,"\n") time.sleep(2) Hills() elif sword == "no": print("Old Joe Smith: Well, if you say so... ...good luck anyway!\n") Hills() else: print("Old Joe Smith: I'm sorry, what?") time.sleep(1) Menu() def Hills(): print("*You walk through the forest, when out of nowhere a minotaur appears!*") fight = input("What will you do, run or fight? ") fight = fight.lower() if fight == "run": print("You escape, barely.") Cave() if fight == "fight": input("Press enter") if damage > 5: print("You win! you looted 10 gold and got 5 xp.") gold = gold + 10 xp = xp + 5 Cave() elif damage < 5: print("You died. Game over.") Menu() else: print("How the hell did you get exactly 5 damage?") Menu() else: print("Your lack of a proper response makes the minotaur charge. It kills you instantly.") Menu() def Cave(): print("You stumble into a cave. there are two routes in the cave. which way do you want to go, left or right?") print("Welcome to 'RPG Game', a role-playing game developed in Python 3.3.\nThis game was developed by bamf_mccree.\n") print("Old Joe Smith: Hello, adventurer, and welcome to Dankhill, in the centre of Whitewood forest. \nThis was once a peaceful place, but the evil Lord Draktha has enslaved most of the civilians of our realm.") time.sleep(4) Menu() Answer: In this case I imagine you only need to declare those variables outside the local method scope and when you modify them use the global keyword. Something like this: import os import random import time damage = None xp = None gold = None def Menu(): damage = 1 print("\nyour attack value is", damage,"\nyour xp level is", xp,"\nand you have", gold,"gold.\n") sword = input("Old Joe Smith: Do you need a sword? ") sword = sword.lower() if sword == "yes": damage = damage + 9 print("Old Joe Smith: *Gives you a sharp steel sword* Good luck on your travels!\n\nYour new attack value is",damage,"\n") time.sleep(2) Hills() if sword == "yeah": damage = damage + 9 print("Old Joe Smith: *Gives you a sharp steel sword* Good luck on your travels!\n\nYour new attack value is",damage,"\n") time.sleep(2) Hills() elif sword == "no": print("Old Joe Smith: Well, if you say so... ...good luck anyway!\n") Hills() else: print("Old Joe Smith: I'm sorry, what?") time.sleep(1) Menu() def Hills(): print("*You walk through the forest, when out of nowhere a minotaur appears!*") fight = input("What will you do, run or fight? ") fight = fight.lower() if fight == "run": print("You escape, barely.") Cave() if fight == "fight": input("Press enter") if damage > 5: print("You win! you looted 10 gold and got 5 xp.") global gold gold = gold + 10 global xp xp = xp + 5 Cave() elif damage < 5: print("You died. Game over.") Menu() else: print("How the hell did you get exactly 5 damage?") Menu() else: print("Your lack of a proper response makes the minotaur charge. It kills you instantly.") Menu() def Cave(): print("You stumble into a cave. there are two routes in the cave. which way do you want to go, left or right?") print("Welcome to 'RPG Game', a role-playing game developed in Python 3.3.\nThis game was developed by bamf_mccree.\n") print("Old Joe Smith: Hello, adventurer, and welcome to Dankhill, in the centre of Whitewood forest. \nThis was once a peaceful place, but the evil Lord Draktha has enslaved most of the civilians of our realm.") time.sleep(4) Menu()
How to link yaml key values to json key values in python Question: Hi I would like to essentially use yaml data inside json for eg. json file: { "Name": "foo", "Birthdate": "1/1/1991", "Address": "FOO_ADDRESS", "Note": "Please deliver package to foo at FOO_ADDRESS using COURIER service" } yaml file: --- FOO_ADDRESS: "foo lane, foo state" COURIER: "foodex" Could someone please guide me on the most efficient way to do this? In this particular example I don't really need to use a separate yaml file (I understand that). But in my specific case I might have to do that. Edit: sorry I didnt paste the desired output file Should look something like this: { "Name": "foo", "Birthdate": "1/1/1991", "Address": "foo lane, foo state", "Note": "Please deliver package to foo at foo lane, foo state using foodex service" } Answer: To be safe, first load the JSON and then do the replacements in the loaded strings. If you do the replacements in the JSON source, you might end up with invalid JSON output (when the replacement strings contain `"` or other characters that have to be escaped in JSON). import yaml, json def doReplacements(jsonValue, replacements): if isinstance(jsonValue, dict): processed = {doReplacements(key, replacements): \ doReplacements(value, replacements) for key, value in \ jsonValue.iteritems()} # Python 3: use jsonValue.items() instead elif isinstance(jsonValue, list): processed = [doReplacements(item, replacements) for item in jsonValue] elif isinstance(jsonValue, basestring): # Python 3: use isinstance(jsonValue, str) instead processed = jsonValue for key, value in replacements.iteritems(): # Python 3: use replacements.items() instead processed = processed.replace(key, value) else: # nothing to replace for Boolean, None or numbers processed = jsonValue return processed input = json.loads("""{ "Name": "foo", "Birthdate": "1/1/1991", "Address": "FOO_ADDRESS", "Note": "Please deliver package to foo at FOO_ADDRESS using COURIER service" } """) replacements = yaml.safe_load("""--- FOO_ADDRESS: "foo lane, foo state" COURIER: "foodex" """) print json.dumps(doReplacements(input, replacements), indent=2) # Python 3: `(...)` around print argument Use `json.load` and `json.dump` to read/write files instead of strings. Note that loading and writing the JSON data may change the order of the items in the object (which you should not depend on anyway).
Python - pip install markupsafe Question: So i want to install markupsafe and get this following error: Command "c:\python27\python.exe -u -c "import setuptools, tokenize;__file__='c:\ \users\\appdata\\local\\temp\\pip-build-yb_vy0\\markupsafe\\setup.py';ex ec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n' ), __file__, 'exec'))" install --record c:\users\appdata\local\temp\pip-h xgnlp-record\install-record.txt --single-version-externally-managed --compile" f ailed with error code 1 in c:\users\appdata\local\temp\pip-build-yb_vy0\m arkupsafe\ anyone got an idea how to fix it? Answer: you need install [Microsoft Visual C++ Compiler for Python 2.7](https://www.microsoft.com/en-us/download/details.aspx?id=44266) if you are using windows else sudo apt-get install gcc gcc-c++ if is not OS windows
Is it possible to get all the urls in a chain of redirects for a given link on the client-side using JavaScript? Question: I am currently working on a JavaScript chrome extension and would like to be able to send all the redirects for a given link that matches a certain criteria. I would like to do this client side invisibly and send the data back to an endpoint. There are several ways to do this in other server-side languages (I would like to basically have the functionality of the following python script) import requests response = requests.get(domain) redirects = list() for r in response.history: redirects.append(r.url) Is there anyway to extract this sort of information invisibly on the client- side, preferably using JavaScript/some JS library? Thanks in advance for your time! Answer: This is not possible from the client side using JavaScript. You can send a HTTP request to a certain URL using [XMLHttpRequest](https://developer.mozilla.org/en- US/docs/Web/API/XMLHttpRequest), but it will automatically follow redirects down to the final destination. There is not way to retrieve the redirection history. Also, browsers prevent sending requests to different origins as per the [Same-origin policy](https://en.wikipedia.org/wiki/Same-origin_policy).
use existing field as _id using elasticsearch dsl python DocType Question: I have class, where I try to set `student_id` as `_id` field in [![](//i.stack.imgur.com/817gJ.png)elasticsearch](/questions/tagged/elasticsearch "show questions tagged 'elasticsearch'"). I am referring [persistent example](https://elasticsearch-dsl.readthedocs.io/en/latest/#persistence- example) from elasticsearch-dsl docs. from elasticsearch_dsl import DocType, String ELASTICSEARCH_INDEX = 'student_index' class StudentDoc(DocType): ''' Define mapping for Student type ''' student_id = String(required=True) name = String(null_value='') class Meta: # id = student_id index = ELASTICSEARCH_INDEX I tied by setting `id` in `Meta` but it not works. I get solution as override [`save` method](https://github.com/elastic/elasticsearch-dsl-py/issues/360) and I achieve this def save(self, **kwargs): ''' Override to set metadata id ''' self.meta.id = self.student_id return super(StudentDoc, self).save(**kwargs) I am creating this object as >>> a = StudentDoc(student_id=1, tags=['test']) >>> a.save() Is there any direct way to set from `Meta` without override `save` method ? Answer: There are a few ways to assign an id: You can do it like this a = StudentDoc(meta={'id':1}, student_id=1, tags=['test']) a.save() Like this: a = StudentDoc(student_id=1, tags=['test']) a.meta.id = 1 a.save() Also note that before ES 1.5, one was able to [specify a field](https://www.elastic.co/guide/en/elasticsearch/reference/1.7/mapping-id- field.html#_path) to use as the document `_id` (in your case, it could have been `student_id`), but this has been deprecated in 1.5 and from then onwards you must explicitly provide an ID or let ES pick one for you.
python-plotly-boxplot Why not showing the max and minimum value on graph Question: This is my data:[0, 45, 47, 46, 47,47,43,100]. And picture looks like this: <https://plot.ly/~zfrancica/8/> I want to have a picture like this: <https://plot.ly/~zfrancica/9/> (an out come picture of [0, 45, 47, 46, 100] ) I want to have a fixed 0 minimum and 100 maximum.(May be this is not the correct box plot but I want to fix the minimum and maximum.) How should I do this? (If ploty can't do this, a matplotlib plot code would also be fine.) My code: import plotly.plotly as py import plotly.graph_objs as go def box_plot(**kwargs): print kwargs num = len(kwargs['circum']) #WornPercentage = go.Box(x=kwargs['circum']) #data = [WornPercentage] #py.iplot(data) data = [ go.Box( x=kwargs['circum'], boxpoints='all', jitter=0.3, pointpos=-1.8 ) ] py.iplot(data) process_dict={'circum':[0, 45, 47, 46, 47,47,43,100]} box_plot(**process_dict) Answer: ### Credentials For those who don't know how to save credentials permanently; import plotly.tools as tls tls.set_credentials_file(username='username', api_key='api-key') Assuming you have already saved your credentials; ### A - Simple Solution For a single one dimensional data; import plotly.plotly as py import plotly.graph_objs as go trace0 = go.Box( y=[0, 45, 47, 46, 47, 47, 43, 100], # Data provided name = 'Provided Data Chart', marker = dict( color = 'rgb(0, 0, 255)' # Blue : #0000FF ) ) data = [trace0] py.plot(data) # Using plot instead of iplot ### A - Output [![enter image description here](http://i.stack.imgur.com/VaUWd.png)](http://i.stack.imgur.com/VaUWd.png) ### B - Using Layout And you can also check this out for a nicer layout, there is a `range` option you can use, for when `autorange` is set to `False`; import plotly.plotly as py import plotly.graph_objs as go from plotly.graph_objs import Layout from plotly.graph_objs import Font from plotly.graph_objs import YAxis from plotly.graph_objs import Figure trace0 = go.Box( y=[0, 45, 47, 46, 47, 47, 43, 100], # Data provided name = 'Provided Data Chart', marker = dict( color = 'rgb(0, 0, 255)' # Blue : #00FF00 ) ) data = [trace0] layout = Layout( autosize=True, font=Font( family='"Droid Sans", sans-serif' ), height=638, title='Plotly Box Demo', width=1002, yaxis=YAxis( autorange=False, # Autorange set to False range=[0, 100], # Custom Range title='Y units', # To be changed, units type='linear' # Use 'log' for appropriate data ) ) fig = Figure(data=data, layout=layout) plot_url = py.plot(fig) # Using plot instead of iplot ### B - Output [![enter image description here](http://i.stack.imgur.com/ioAWj.png)](http://i.stack.imgur.com/ioAWj.png) Hope that it helps.
Python Inheritance suitability of using subclass to produce plot after setting plot properties in parent Question: I am trying to write a simple Python matplotlib plotting Class that will set up formatting of the plot as I would like. To do this, I am using a class and a subclass. The parent generates an axis and figure, then sets all axis properties that I would like. Then the `child` generates the plot itself - scatter plot, line, etc. - and saves the plot to an output file. Here is what I have come up with for the purposes of this question (the plot itself is based on [this code](http://matplotlib.org/examples/pylab_examples/simple_plot.html)): import matplotlib.pyplot as plt import numpy as np class myplot(): def __init__(self,dims,padd): self.fig = plt.figure(figsize=(8.5, 11)) self.ax = self.fig.add_axes(dims) self.pad = padd def ax_prop(self): self.ax.set_axisbelow(True) self.ax.tick_params(axis='x', pad=self.pad, which='both') class plotter(myplot): def __init__(self,dims,padd,x,y): myplot.__init__(self,dims,padd) self.x = x self.y = y def mk_p(self): self.ax.plot(self.x, self.y, linestyle = '-') plt.savefig('Outfile.png', facecolor=self.fig.get_facecolor(), dpi = 300) plt.show() if __name__ == "__main__": x = np.arange(0.0, 2.0, 0.01) y = np.sin(2*np.pi*x) propr = [0.60, 0.2, 0.2, 0.25]; padding =5 plt_instance = myplot(propr,padding) plt_instance.ax_prop() pl_d = plotter(propr,padding,x,y) pl_d.mk_p() I'm tying to base this on the following: The child must have all the properties of the parent. Therefore, the `child` class `plotter()` must inherit the: ax, and all its customized properties fig from the parent (`myplot()`) class. The following may change in the `Child` class: 1. type of plot - scatter, line, etc. 2. any properties of the type of plot - `markersize`, `linestyle`, etc. but the `matplotlib` figure and axis objects (`fig`,`ax`) will always be required before plotting an input set of numbers. **Question:** Is this a _correct_ usage of Python inheritance or is the child class superfluous in this case? If so, was there a place where the reasoning for a subclass was not consistent with object oriented programming (I'm having difficulty convincing myself of this)? Answer: I think the main issue is that your subclass violates the [Liskov Substitution Principle](https://en.wikipedia.org/wiki/Liskov_substitution_principle). Also see [here](https://lostechies.com/derickbailey/2009/02/11/solid-development- principles-in-motivational-pictures/) for nice pictures of SOLID principles. You can't substitute an instance of `myplot` for `plotter`, since `plotter`'s method signature is different. You should be able to substitute in a subclass instead of the base class. Because the signature is different, you cannot.
How can I debug a connection issue within VOLTTRON? Question: I am connecting to an external VOLTTRON instance. I am not getting a response from the connection. What's the issue? I am writing a simple python script to connect to an external platform and retrieve the peers. If I get the serverkey, clientkey, and/or publickey incorrect I don't know how to determine which is the culprit, from the client side. I just get a gevent timeout. Is there a way to know? import os import gevent from volttron.platform.vip.agent import Agent secret = "secret" public = "public" serverkey = "server" tcp_address = "tcp://external:22916" agent = Agent(address=tcp_address, serverkey=serverkey, secretkey=secret, publickey=public) event = gevent.event.Event() greenlet = gevent.spawn(agent.core.run, event) event.wait(timeout=30) print("My id: {}".format(agent.core.identity)) peers = agent.vip.peerlist().get(timeout=5) for p in peers: print(p) gevent.sleep(3) greenlet.kill() Answer: The short answer: no, the client cannot determine why its connection to the server failed. The client will attempt to connect until it times out. Logs and debug messages on the _server side_ can help troubleshoot a connection problem. There are three distinct messages related to key errors: 1. `CURVE I: cannot open client HELLO -- wrong server key?` Either the client omit the server key, the client used the wrong server key, or the server omit the secret key. 2. `CURVE I: cannot open client INITIATE vouch` Either the client omit the public or secret key, or its public and secret keys don't correspond to each other. 3. `authentication failure` The server key was correct and the secret and public keys are valid, but the server rejected the connection because the client was not authorized to connect (based on the client's public key). The first two messages are printed by [libzmq](https://github.com/zeromq/libzmq/blob/master/src/curve_server.cpp). To see the third message `volttron` must be started with increased verboseness (at least `-v`).
Stacked Bar Chart in Matplotlib; Series Are Overlaying Instead Of Stacking Question: Struggling to figure this one out. I have a stacked bar chart that I'm trying to create with Python / Matplotlib and it seems to be overlaying the data instead of stacking resulting in different colors (i.e. red+yellow = orange instead of stacking red and yellow). Can anyone see what I'm doing wrong? Here's my code: #Stacked Bar Char- matplotlib #Create the general blog and the "subplots" i.e. the bars f, ax1 = plt.subplots(1, figsize=(10,5)) # Set the bar width bar_width = 0.75 # positions of the left bar-boundaries bar_l = [i+1 for i in range(len(df4['bikes']))] # positions of the x-axis ticks (center of the bars as bar labels) tick_pos = [i+(bar_width/2) for i in bar_l] # Create a bar plot, in position bar_1 ax1.bar(bar_l, df4['bikes1'], width=bar_width, label='bikes', alpha=0.5,color='r', align='center') # Create a bar plot, in position bar_1 ax1.bar(bar_l, df4['bikes'], width=bar_width,alpha=0.5,color='r', align='center') # Create a bar plot, in position bar_1 ax1.bar(bar_l, df4['cats1'], width=bar_width, label='cats', alpha=0.5,color='y', align='center') # Create a bar plot, in position bar_1 ax1.bar(bar_l, df4['cats'], width=bar_width,alpha=0.5,color='y', align='center') # set the x ticks with names plt.xticks(tick_pos, df4['Year']) # Set the label and legends ax1.set_ylabel("Count") ax1.set_xlabel("Year") plt.legend(loc='upper left') ax1.axhline(y=0, color='k') ax1.axvline(x=0, color='k') # Set a buffer around the edge plt.xlim([min(tick_pos)-bar_width, max(tick_pos)+bar_width]) plt.show() [![enter image description here](http://i.stack.imgur.com/XyCyD.png)](http://i.stack.imgur.com/XyCyD.png) Answer: You have to manually calculate position from which your _cats_ bar will start (look at the `bottom` variable in my code when _cats_ are plotting). You have to propose your own method to calculate position of _cats_ bars depending on data in your dataframe. Your colors are mixed up because you use `alpha` variable and when bars overlayed then colors would mixed up: import matplotlib.pylab as plt import numpy as np import pandas as pd df4 = pd.DataFrame.from_dict({'bikes1':[-1,-2,-3,-4,-5,-3], 'bikes':[10,20,30,15,11,11], 'cats':[1,2,3,4,5,6], 'cats1':[-6,-5,-4,-3,-2,-1], 'Year': [2012,2013,2014,2015,2016,2017]}) #Create the general blog and the "subplots" i.e. the bars f, ax1 = plt.subplots(1, figsize=(10,5)) # Set the bar width bar_width = 0.75 # positions of the left bar-boundaries bar_l = [i+1 for i in range(len(df4['bikes']))] # positions of the x-axis ticks (center of the bars as bar labels) tick_pos = [i+(bar_width/2) for i in bar_l] # Create a bar plot, in position bar_1 ax1.bar(bar_l, df4['bikes1'], width=bar_width, label='bikes', alpha=0.5,color='r', align='center') # Create a bar plot, in position bar_1 ax1.bar(bar_l, df4['bikes'], width=bar_width,alpha=0.5,color='r', align='center') # Create a bar plot, in position bar_1 ax1.bar(bar_l, df4['cats1'], width=bar_width, label='cats', alpha=0.5,color='y', align='center', bottom=[min(i,j) for i,j in zip(df4['bikes'],df4['bikes1'])]) # Create a bar plot, in position bar_1 ax1.bar(bar_l, df4['cats'], width=bar_width,alpha=0.5,color='y', align='center', bottom=[max(i,j) for i,j in zip(df4['bikes'],df4['bikes1'])]) # set the x ticks with names plt.xticks(tick_pos, df4['Year']) # Set the label and legends ax1.set_ylabel("Count") ax1.set_xlabel("Year") plt.legend(loc='upper left') ax1.axhline(y=0, color='k') ax1.axvline(x=0, color='k') # Set a buffer around the edge plt.xlim([min(tick_pos)-bar_width, max(tick_pos)+bar_width]) plt.show() [![enter image description here](http://i.stack.imgur.com/6WP70.png)](http://i.stack.imgur.com/6WP70.png)
Two python pools, two queues Question: I am trying to understand how pool and queue work in Python, and the following example doesn't work as expected. I expect the program to end, but it's stuck in an infinite loop because the second queue isn't getting emptied. import multiprocessing import os import time inq = multiprocessing.Queue() outq = multiprocessing.Queue() def worker_main(q1, q2): while True: i = q1.get(True) time.sleep(.1) q2.put(i*2) def worker2(q): print q.get(True) p1 = multiprocessing.Pool(3, worker_main,(inq, outq,)) p2 = multiprocessing.Pool(2, worker2,(outq,)) for i in range(50): inq.put(i) while inq.qsize()>0 or outq.qsize()>0: print 'q1 size', inq.qsize(), 'q2 size', outq.qsize() time.sleep(.1) the output shows that the second queue (outq) is .get once, but that's all. output: > > q1 size 49 q2 size 0 > q1 size 47 q2 size 0 > 2 > 4 > q1 size 44 q2 size 1 > q1 size 41 q2 size 4 > q1 size 38 q2 size 7 > q1 size 35 q2 size 11 > q1 size 31 q2 size 14 > q1 size 27 q2 size 18 > q1 size 24 q2 size 21 > q1 size 22 q2 size 23 > q1 size 19 q2 size 26 > q1 size 15 q2 size 30 > q1 size 12 q2 size > Why isn't the worker2 getting called until the outq is empty? Answer: This is a very odd way to use a `Pool`. The function passed to the constructor is called only once per process in the pool. It's intended for one-time initialization tasks, and is rarely used. As is, your `worker2` is called exactly twice, one time for each process in yout `p2` pool. Your function gets one value from a queue and then exits. The process never does anything else. So it's doing exactly what you coded it to do. There's no evident reason to use a `Pool` here at all; creating 5 `multiprocessing.Process` objects instead would be more natural. If you feel you have to do it this way, then you need to put a loop in `worker2`. Here's one way: import multiprocessing import time def worker_main(q1, q2): while True: i = q1.get() if i is None: break time.sleep(.1) q2.put(i*2) def worker2(q): while True: print(q.get()) if __name__ == "__main__": inq = multiprocessing.Queue() outq = multiprocessing.Queue() p1 = multiprocessing.Pool(3, worker_main,(inq, outq,)) p2 = multiprocessing.Pool(2, worker2,(outq,)) for i in range(50): inq.put(i) for i in range(3): # tell worker_main we're done inq.put(None) while inq.qsize()>0 or outq.qsize()>0: print('q1 size', inq.qsize(), 'q2 size', outq.qsize()) time.sleep(.1) ## SUGGESTED This is a "more natural" way to use `Process` objects instead, and using queue sentinels (special values - here `None`) to let processes know when to stop. BTW, I'm using Python 3, so use `print` as a function rather than as a statement. import multiprocessing as mp import time def worker_main(q1, q2): while True: i = q1.get() if i is None: break time.sleep(.1) q2.put(i*2) def worker2(q): while True: i = q.get() if i is None: break print(i) def wait(procs): alive_count = len(procs) while alive_count: alive_count = 0 for p in procs: if p.is_alive(): p.join(timeout=0.1) print('q1 size', inq.qsize(), 'q2 size', outq.qsize()) alive_count += 1 if __name__ == "__main__": inq = mp.Queue() outq = mp.Queue() p1s = [mp.Process(target=worker_main, args=(inq, outq,)) for i in range(3)] p2s = [mp.Process(target=worker2, args=(outq,)) for i in range(2)] for p in p1s + p2s: p.start() for i in range(50): inq.put(i) for p in p1s: # tell worker_main we're done inq.put(None) wait(p1s) # Tell worker2 we're done for p in p2s: outq.put(None) wait(p2s)
Why do python Subprocess.stdin.write() kill PyQt Gui Question: I have a PyQt5 app that need to write input for the subprocess to stop. However it also kills my PyQt5 `Mainwindow`, if input button is used without using subprocess button first. If i use subprocess button first, and then use input button , with `self.bob.stdin.write("b")` app stays open, but if i press input button first without pressing subprocess button `self.bob.stdin.write("b")` it kills my app and Mainwindow. So why do `self.bob.stdin.write("b")` kill app ,and how do i stop it from killing my MainWindow, if i press input button first ? The dilemma can be seen in this test code. from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(308, 156) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.Button_2 = QtWidgets.QPushButton(self.centralwidget) self.Button_2.setGeometry(QtCore.QRect(20, 40, 121, 71)) self.Button_2.setObjectName("subprocessButton_2") self.Button_1 = QtWidgets.QPushButton(self.centralwidget) self.Button_1.setGeometry(QtCore.QRect(170, 40, 121, 71)) self.Button_1.setObjectName("inputbutton") MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate self.Button_2.setText(_translate("MainWindow", "subprocess")) self.Button_1.setText(_translate("MainWindow", "input")) self.Button_2.clicked.connect(self.sub) self.Button_1.clicked.connect(self.input) def sub(self): import subprocess from subprocess import Popen, PIPE from subprocess import Popen, PIPE self.bob = subprocess.Popen('cmd ',stdin=PIPE, shell=True) def input(self): self.bob.stdin.write("q") if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_()) Answer: Without understanding all your code, here's what I think your code converted into a script does: import subprocess from subprocess import Popen, PIPE # sub button handler bob = subprocess.Popen('echo',stdin=PIPE, shell=True) # input button handler bob.stdin.write(b"text") I'll let you think about what hapens when you don't do step 1.
Caffe always gives the same prediction in Python, but training accuracy is good Question: I have a trained model with caffe (through the command line) where I get an **accuracy of 63%** (according to the log files). However, when I try to run a script in Python to test the accuracy, I get all the predictions in the same class, with very similar prediction values, but not quite identical. My goal is to compute the accuracy per class. Here are some examples of predictions: [ 0.20748076 0.20283087 0.04773897 0.28503627 0.04591063 0.21100247] (label 0) [ 0.21177764 0.20092578 0.04866471 0.28302929 0.04671735 0.20888527] (label 4) [ 0.19711637 0.20476575 0.04688895 0.28988105 0.0465695 0.21477833] (label 3) [ 0.21062914 0.20984225 0.04802448 0.26924771 0.05020727 0.21204917] (label 1) Here is the prediction script (only the part which gives the predictions for a specific image): import numpy as np import matplotlib.pyplot as plt import sys import os import caffe caffe.set_device(0) caffe.set_mode_gpu() # Prepare Network MODEL_FILE = 'finetune_deploy.prototxt' PRETRAINED = 'finetune_lr3_iter_25800.caffemodel.h5' MEAN_FILE = 'balanced_dataset_256/Training/labels_mean/trainingMean_original.binaryproto' blob = caffe.proto.caffe_pb2.BlobProto() dataBlob = open( MEAN_FILE , 'rb' ).read() blob.ParseFromString(dataBlob) dataMeanArray = np.array(caffe.io.blobproto_to_array(blob)) mu = dataMeanArray[0].mean(1).mean(1) net = caffe.Classifier(MODEL_FILE, PRETRAINED, mean=mu, channel_swap=(2,1,0), raw_scale=255, image_dims=(256, 256)) PREFIX='balanced_dataset_256/PrivateTest/' LABEL = '1' imgName = '33408.jpg' IMAGE_PATH = PREFIX + LABEL + '/' + imgName input_image = caffe.io.load_image(IMAGE_PATH) plt.imshow(input_image) prediction = net.predict([input_image]) # predict takes any number of images, and formats them for the Caffe net automatically print 'prediction shape:', prediction[0].shape plt.plot(prediction[0]) print 'predicted class:', prediction[0].argmax() print prediction[0] The input data is **grayscale** , but I convert it to **RGB** by duplicating the channels. Here's the architecture file finetune_deploy.prototxt: name: "FlickrStyleCaffeNetTest" layer { name: "data" type: "Input" top: "data" # top: "label" input_param { shape: { dim: 1 dim: 3 dim: 256 dim: 256 } } } layer { name: "conv1" type: "Convolution" bottom: "data" top: "conv1" param { lr_mult: 1 decay_mult: 1 } param { lr_mult: 2 decay_mult: 0 } convolution_param { num_output: 96 kernel_size: 11 stride: 4 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" value: 0 } } } layer { name: "relu1" type: "ReLU" bottom: "conv1" top: "conv1" } layer { name: "pool1" type: "Pooling" bottom: "conv1" top: "pool1" pooling_param { pool: MAX kernel_size: 3 stride: 2 } } layer { name: "norm1" type: "LRN" bottom: "pool1" top: "norm1" lrn_param { local_size: 5 alpha: 0.0001 beta: 0.75 } } layer { name: "conv2" type: "Convolution" bottom: "norm1" top: "conv2" param { lr_mult: 1 decay_mult: 1 } param { lr_mult: 2 decay_mult: 0 } convolution_param { num_output: 256 pad: 2 kernel_size: 5 group: 2 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" value: 1 } } } layer { name: "relu2" type: "ReLU" bottom: "conv2" top: "conv2" } layer { name: "pool2" type: "Pooling" bottom: "conv2" top: "pool2" pooling_param { pool: MAX kernel_size: 3 stride: 2 } } layer { name: "norm2" type: "LRN" bottom: "pool2" top: "norm2" lrn_param { local_size: 5 alpha: 0.0001 beta: 0.75 } } layer { name: "conv3" type: "Convolution" bottom: "norm2" top: "conv3" param { lr_mult: 1 decay_mult: 1 } param { lr_mult: 2 decay_mult: 0 } convolution_param { num_output: 384 pad: 1 kernel_size: 3 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" value: 0 } } } layer { name: "relu3" type: "ReLU" bottom: "conv3" top: "conv3" } layer { name: "conv4" type: "Convolution" bottom: "conv3" top: "conv4" param { lr_mult: 1 decay_mult: 1 } param { lr_mult: 2 decay_mult: 0 } convolution_param { num_output: 384 pad: 1 kernel_size: 3 group: 2 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" value: 1 } } } layer { name: "relu4" type: "ReLU" bottom: "conv4" top: "conv4" } layer { name: "conv5" type: "Convolution" bottom: "conv4" top: "conv5" param { lr_mult: 1 decay_mult: 1 } param { lr_mult: 2 decay_mult: 0 } convolution_param { num_output: 256 pad: 1 kernel_size: 3 group: 2 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" value: 1 } } } layer { name: "relu5" type: "ReLU" bottom: "conv5" top: "conv5" } layer { name: "pool5" type: "Pooling" bottom: "conv5" top: "pool5" pooling_param { pool: MAX kernel_size: 3 stride: 2 } } layer { name: "fc6" type: "InnerProduct" bottom: "pool5" top: "fc6" param { lr_mult: 1 decay_mult: 1 } param { lr_mult: 2 decay_mult: 0 } inner_product_param { num_output: 4096 weight_filler { type: "gaussian" std: 0.005 } bias_filler { type: "constant" value: 1 } } } layer { name: "relu6" type: "ReLU" bottom: "fc6" top: "fc6" } layer { name: "drop6" type: "Dropout" bottom: "fc6" top: "fc6" dropout_param { dropout_ratio: 0.5 } } layer { name: "fc7" type: "InnerProduct" bottom: "fc6" top: "fc7" # Note that lr_mult can be set to 0 to disable any fine-tuning of this, and any other, layer param { lr_mult: 1 decay_mult: 1 } param { lr_mult: 2 decay_mult: 0 } inner_product_param { num_output: 4096 weight_filler { type: "gaussian" std: 0.005 } bias_filler { type: "constant" value: 1 } } } layer { name: "relu7" type: "ReLU" bottom: "fc7" top: "fc7" } layer { name: "drop7" type: "Dropout" bottom: "fc7" top: "fc7" dropout_param { dropout_ratio: 0.5 } } layer { name: "fc8_flickr" type: "InnerProduct" bottom: "fc7" top: "fc8_flickr" # lr_mult is set to higher than for other layers, because this layer is starting from random while the others are already trained param { lr_mult: 10 decay_mult: 1 } param { lr_mult: 20 decay_mult: 0 } inner_product_param { num_output: 6 weight_filler { type: "gaussian" std: 0.01 } bias_filler { type: "constant" value: 0 } } } layer { name: "prob" type: "Softmax" bottom: "fc8_flickr" top: "prob" } Answer: I figured it out, the deploy.prototxt had some input_param mismatch. Strange behavior.
How to generate permutations without generating repeating results but with a fixed amount of characters Python Question: I am trying to figure out a way to generate all permutations possible of a string that has a couple repeating characters but without generating repeated tuples. Right now I am using `itertools.permutations()`. It works but I need to remove repetition and I **cannot** use `set()` to remove the repetition. What kind of results am I expecting? Well, for example, I want to get all the combinations for `DDRR`, the thing with `itertools.permutations()` is that I would get `DDRR` about four times, given that `itertools` sees the `D`s as if they were different, same with `R`s. With `list(itertools.permutations('DDRR'))` I get: [('D', 'D', 'R', 'R'), ('D', 'D', 'R', 'R'), ('D', 'R', 'D', 'R'), ('D', 'R', 'R', 'D'), ('D', 'R', 'D', 'R'), ('D', 'R', 'R', 'D'), ('D', 'D', 'R', 'R'), ('D', 'D', 'R', 'R'), ('D', 'R', 'D', 'R'), ('D', 'R', 'R', 'D'), ('D', 'R', 'D', 'R'), ('D', 'R', 'R', 'D'), ('R', 'D', 'D', 'R'), ('R', 'D', 'R', 'D'), ('R', 'D', 'D', 'R'), ('R', 'D', 'R', 'D'), ('R', 'R', 'D', 'D'), ('R', 'R', 'D', 'D'), ('R', 'D', 'D', 'R'), ('R', 'D', 'R', 'D'), ('R', 'D', 'D', 'R'), ('R', 'D', 'R', 'D'), ('R', 'R', 'D', 'D'), ('R', 'R', 'D', 'D')] The ideal result I want is: [('D', 'R', 'R', 'D'), ('R', 'D', 'R', 'D'), ('R', 'R', 'D', 'D'), ('D', 'R', 'D', 'R'), ('D', 'D', 'R', 'R'), ('R', 'D', 'D', 'R')] Answer: Make use of a set which only generates unique tuples of permutations like so: >>> from itertools import permutations >>> perms = set(permutations('DDRR')) >>> perms set([('R', 'R', 'D', 'D'), ('D', 'R', 'R', 'D'), ('R', 'D', 'R', 'D'), ('D', 'D', 'R', 'R'), ('D', 'R', 'D', 'R'), ('R', 'D', 'D', 'R')]) You can easily convert it back to a list if needed by just doing `list(perms)`.
Execution time of multithreaded python program Question: Considering GIL, I expected this program to finish in 9 seconds, but to my surprise, it ends in 4 seconds. Looking for probable reasons or am I missing something? import time import threading def get_data(start, end): res = [] for i in range(start, end): time.sleep(1) res.append(i) print res range_list = [(1,4), (4,7), (6,10)] for r in range_list: t = threading.Thread(target=get_data, args = (r[0], r[1])) t.start() Time of execution:- Without threading - 9sec With threading - 4 sec Answer: Normally, if you don't use multithreading this program finish in 9 seconds because python runs lines one by one so when you put time.sleep(1), python just wait one second and on the other hand do not anything. But when you use multithreading the program runs thread functions separately. So for example if you call thread function 2 times, the thread function runs line by line seperately in the same time. In this program, you call thread function for 3 times. First call, python wait 3 seconds for i =1,i=2,i=3 and second call, python wait 3 seconds for i =4,i=5,i=6 and final call python wait 4 seconds for i =6,i=7,i=8,i=9. This codes runs separately each other so this program finish in 4 second because the biggest time is 4 seconds.
regular expression match issue in Python Question: For input string, want to match text which starts with `{(P)` and ends with `(P)}`, and I just want to match the parts in the middle. Wondering if we can write one regular expression to resolve this issue? For example, in the following example, for the input string, I want to retrieve _hello world_ part. Using Python 2.7. python {(P)hello world(P)} java Answer: You can try `{\(P\)(.*)\(P\)}`, and use parenthesis in the pattern to capture everything between `{(P)` and `(P)}`: import re re.findall(r'{\(P\)(.*)\(P\)}', "python {(P)hello world(P)} java") # ['hello world'] `.*` also matches unicode characters, for example: import re str1 = "python {(P)£1,073,142.68(P)} java" str2 = re.findall(r'{\(P\)(.*)\(P\)}', str1)[0] str2 # '\xc2\xa31,073,142.68' print str2 # £1,073,142.68
How to iterate over all these possibilities? Question: Assume we have a python list list = [[1,2,3],[4,5,6],[7,8,9]] I define the sum as the following, sum: is the total sum of single entries (different index) from each of the sublists. This sounds complex so I will give an example, for the above list, 1 + 5 + 9 is one of the sums because 1 is from the first sublist and 5 is from the second sublist and 9 is from the 3rd sublist and they all have different positions in their corresponding sublist. so i can't have `1 + 4 + 7` because 1,4 & 7 are first entries in their sublists. I can't have `1 + 5 + 8` because 5 & 8 are both second entries in their list and so on for example, and I want to find the highest sum of the total of individual entries of each of the sublist !! How can I iterate over all these possible sums and then get the highest out of all these sums. For the above list, we have 3^3=27 different sums. And is there an efficient way to do it with python ? Answer: This is a classic problem that can be solved using [Hungarian algorithm](https://en.wikipedia.org/wiki/Hungarian_algorithm). There is an implementation in sklearn: from sklearn.utils.linear_assignment_ import linear_assignment import numpy as np M = [[1,2,3],[4,5,6],[7,8,9]] M = np.array(M) #convert to numpy array result = linear_assignment(M) answer = sum(M[cell[0]][cell[1]] for cell in result) Iterate over all possible sums is a bad idea (O(N!)). The algorithm above must run in O(N^3).
Selenium Python Script throws no element error exception even though the x path is right? Question: from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys driver=webdriver.Chrome() driver.get("https://paytm.com/") driver.maximize_window() driver.find_element_by_class_name("login").click() driver.implicitly_wait(10) driver.find_element_by_xpath("//md-input-container[@class='md-default-theme md-input-invalid']/input[@id='input_0']").send_keys("99991221212") In the above code, I have verified the xpath using fire bug its highlighting the correct element. But when the script run its failing? Can you help me folks? Answer: In selenium each frame is treated individually. Since the login is in a separate `iframe` element, you need to switch to it first using: iframe = driver.find_elements_by_tag_name('iframe')[0] driver.switch_to_frame(iframe) **Before** trying to interact with it's elements. Or in this case, you would wait for the frame to exist, and it would be: from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome() driver.get("https://paytm.com/") driver.maximize_window() wait = WebDriverWait(driver, 10) wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "login"))).click() wait.until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME, "iframe"))) _input = wait.until(EC.visibility_of_element_located((By.ID,"input_0"))) _input.send_keys("99991221212")
Python - Checking whether any of multiple conditions are true Question: I am trying to modify a list in place to remove entries where the file extension does not match. I got this working by writing multiple 'or' conditions from os import listdir image_extensions = ['jpg','.png'] files = listdir('/home') files = [x for x in files if '.jpg' in x or '.png' in x] print files I would like to have this use the image_extensions variable so that I can easily add more conditions. The latest thing I tried fails with the "requires string as left operand, not list": from os import listdir image_extensions = ['jpg','.png'] files = listdir('/home') files = [x for x in files if any(s for s in image_extensions in x)] print files Answer: This should do: files = [x for x in files if any(s in x for s in image_extensions)] # |<-->| Check if _any_ of the extensions is contained in the file name `x`
Python logging message not getting output as expected in simplest scenario Question: I am not getting any output from `logging.debug` in the simplest possible situation. (I do get output from `logger.warn`.) import logging logger = logging.getLogger() logger.setLevel(logging.DEBUG) logger.warn('Logger is warning') logger.debug('Logger is debugging') print(logger.getEffectiveLevel(), logger.isEnabledFor(logging.DEBUG), file=sys.stderr) The last line prints the logger's logging level, which shows as `logging.DEBUG`, and whether the logger is enabled for that level, which is true. yet output appears for `log.warn` but not for `log.debug`. What am I missing? [Python 3.5, OS X 10.11] Answer: The reason is that you haven't specified a handler. On Python 3.5, an internal ["handler of last resort"](https://docs.python.org/3.2/library/logging.html#logging.lastResort) is used when no handler is specified by you, but that internal handler has a level of `WARNING` and so won't show messages of lower severity. You need to specify a handler and add it like this example: logger.addHandler(logging.StreamHandler()) either just before or just after the `setLevel` statement in your snippet.
Beautfil Soup Error with simple script Question: I am running Beautiful Soup 4.5 with Python 3.4 on Windows 7. Here is my script: from bs4 import BeautifulSoup import urllib3 http = urllib3.PoolManager() url = 'https://scholar.google.com' response = http.request('GET', url) html2 = response.read() soup = BeautifulSoup([html2]) print (type(soup)) Here is the error I am getting: > TypeError: Expected String or Buffer I have researched and there seem to be no fixes except going to an older version of Beautiful Soup which I don't want to do. Any help would be much appreciated. Answer: Not sure why are you putting the html string into the list here: soup = BeautifulSoup([html2]) Replace it with: soup = BeautifulSoup(html2) Or, you can also pass the response file-like object, `BeautifulSoup` would read it for you: response = http.request('GET', url) soup = BeautifulSoup(response) * * * It is also a good idea to _specify a parser explicitly_ : soup = BeautifulSoup(html2, "html.parser")
No twitter Module error [Mac] Question: I have a python script that follows/unfollows and favorites tweets. I keep getting an error: from twitter import Twitter, OAuth, TwitterHTTPError ImportError: No module named twitter I put in all the oauth info in my script and i continue to get this error. I think im being blocked from the api. Can someone help me out? Answer: First of all: which library do you use? I expect you use tweepy so from a terminal type: pip install Tweepy
How to automate dictionary creation in Python Question: I am trying to write a python code that solves a Sudoku puzzle. My code starts by making a list of each row/column combination, or the coordinates of each box. Next, I want to find a way to, for each box, reference its location. This is my current code: boxes = [] for i in range(1, 10): for x in range(1,10): boxes = boxes + ['r'+str(i)+'c'+str(x)] for box in boxes: Next, I was going to create a dictionary for each one, but I would want each to be named by the list item. The dictionaries would be, for example, r1c1 = {'row': '1', 'Column': 1}. What is the best way to separate and store this information? Answer: You don't need to create all those dictionaries. You already have your coordinates, just don't lock them up in strings: boxes = [] for i in range(1, 10): for x in range(1,10): boxes.append((i, x)) would create a list of `(row, column)` tuples instead, and you then wouldn't have to map them back. Even if you needed to associate strings with data, you could do so in a _nested_ dictionary: coordinates = { 'r1c1': {'row': 1, 'column': 1}, # ... } but you could also parse that string and extract the numbers after `r` and `c` to produce the row and column numbers again. In fact, I wrote a Sudoku checker on the same principles once; in the following code `block_indices`, `per9()` and `zip(*per9(s))` produce indices for each block, row or column of a puzzle, letting you verify that you have 9 unique values in each. The only difference is that instead of a matrix, I used _one long list_ to represent a puzzle, all elements from row to row included in sequence: from itertools import product block_indices = [[x + y + s for s in (0, 1, 2, 9, 10, 11, 18, 19, 20)] for x, y in product(range(0, 81, 27), range(0, 9, 3))] def per9(iterable): # group iterable in chunks of 9 return zip(*([iter(iterable)] * 9)) def is_valid_sudoku(s): return ( # rows all(len(set(r)) == 9 for r in per9(s)) and # columns all(len(set(c)) == 9 for c in zip(*per9(s))) and # blocks all(len(set(s[i] for i in ix)) == 9 for ix in block_indices) ) So row 1, column 4 is 1 * 9 + 4 = index 13 in a flat list.
Tensorflow memory leak with Recurrent Neural Network Question: I'm trying to build a simple Recurrent Neural Network model by using tensorflow on Mac OS X. It is just a toy model and size of input data doesn't exceed 3MB so it should not consume much of memory. However, when I run model, the memory usage significantly increases every training batch and goes above 10GB. It was for only two iterations. I couldn't run it more. Here's the whole code. from __future__ import absolute_import from __future__ import division from __future__ import print_function import tensorflow as tf import numpy as np from pympler import summary class RNN(): """The RNN model.""" #@profile def inference(self): """calculate outputs and loss for a single batch""" total_loss = 0.0 outputs = [] for i in range(self.batch_size): state = self.init_state outputs.append([]) loss = 0.0 for j in range(self.num_steps): state, output = self.next_state(self.x[i,j,:],state) outputs[i].append(output) loss += tf.square(self.y[i,j,:]-output) total_loss+=loss return outputs, total_loss / (self.batch_size*self.num_steps) def __init__(self, is_training, config): self.sess = sess = tf.Session() self.prev_see = prev_see = config.prev_see self.num_steps = num_steps = config.num_steps #maybe "self.num_hidden =" part could be removed self.num_hidden = num_hidden = config.num_hidden self.batch_size = config.batch_size self.epoch = config.epoch self.learning_rate = config.learning_rate self.summaries_dir = config.summaries_dir with tf.name_scope('placeholders'): self.x = tf.placeholder(tf.float32,[None,num_steps,config.prev_see], name='input-x') self.y = tf.placeholder(tf.float32, [None,num_steps,1],name='input-y') default_init_state = tf.zeros([num_hidden]) self.init_state = tf.placeholder_with_default(default_init_state,[num_hidden], name='state_placeholder') def weight_variable(self,shape): """Create a weight variable with appropriate initialization.""" initial = tf.truncated_normal(shape,stddev=0.1) return tf.Variable(initial) def bias_variable(self,shape): """Create a bias variable with appropriate initialization.""" initial = tf.constant(0.1,shape=shape) return tf.Variable(initial) def variable_summaries(self,var,name): """Attach a lot of summaries to a Tensor.""" with tf.name_scope('summaries'): mean = tf.reduce_mean(var) tf.scalar_summary('mean/'+name,mean) with tf.name_scope('stddev'): stddev = tf.sqrt(tf.reduce_sum(tf.square(var-mean))) tf.scalar_summary('stddev/'+name,stddev) tf.scalar_summary('max/'+name, tf.reduce_max(var)) tf.scalar_summary('min/'+name, tf.reduce_min(var)) tf.histogram_summary(name, var) #declare weight variables as property layer_name = 'rnn_layer' with tf.name_scope(layer_name): with tf.name_scope('U'): self.U = U = weight_variable(self,[prev_see,num_hidden]) variable_summaries(self,U,layer_name+'/U') with tf.name_scope('W'): self.W = W = weight_variable(self,[num_hidden,num_hidden]) variable_summaries(self,W,layer_name+'/W') with tf.name_scope('b_W'): self.b_W = b_W = bias_variable(self,[num_hidden]) variable_summaries(self,b_W,layer_name+'/b_W') with tf.name_scope('V'): self.V = V = weight_variable(self,[num_hidden,1]) variable_summaries(self,V,layer_name+'/V') with tf.name_scope('b_V'): self.b_V = b_V = bias_variable(self,[1]) variable_summaries(self,b_V,layer_name+'/b_V') self.merged = tf.merge_all_summaries() self.train_writer = tf.train.SummaryWriter(config.summaries_dir,sess.graph) tf.initialize_all_variables().run(session=sess) _,self.loss = self.inference() def next_state(self,x,s_prev): """calculate next state and output""" x = tf.reshape(x,[1,-1]) s_prev = tf.reshape(s_prev,[1,-1]) s_next = tf.tanh(tf.matmul(x,self.U)+tf.matmul(s_prev,self.W)+self.b_W) output = tf.matmul(s_next,self.V)+self.b_V return s_next, output #@profile def batch_train(self,feed_dict): """train the network for a single batch""" loss = self.loss train_step = tf.train.GradientDescentOptimizer(self.learning_rate).minimize(loss) summary,loss_value, _ = self.sess.run([self.merged,loss, train_step],feed_dict=feed_dict) #self.train_writer.add_summary(summary) print(loss_value) class TrainConfig(): """Train Config.""" total_steps = 245 test_ratio = 0.3 prev_see = 100 num_steps = int(round((total_steps-prev_see)*(1-test_ratio))) num_hidden = 10 batch_size = 5 epoch = 3 learning_rate = 0.1 summaries_dir = '/Users/Kyungsu/StockPrediction/log' class DebugConfig(): """For debugging memory leak.""" total_steps = 100 test_ratio = 0.3 prev_see = 100 num_steps = 10 num_hidden = 10 batch_size = 5 epoch = 2 learning_rate = 0.1 summaries_dir = '/Users/Kyungsu/StockPrediction/log' #@profile def run_epoch(m,x_data,y_data): num_batch = ((len(x_data)-1) // m.batch_size)+1 #num_batch = 100 for i in range(num_batch): x_batch = x_data[i*m.batch_size:(i+1)*m.batch_size,:,:] y_batch = y_data[i*m.batch_size:(i+1)*m.batch_size,:,:] feed_dict = {m.x:x_batch,m.y:y_batch} print("%dth/%dbatches"%(i+1,num_batch)) m.batch_train(feed_dict) def process_data(data,config): data_size = len(data) prev_see = config.prev_see num_steps = config.num_steps x = np.zeros((data_size,num_steps,prev_see)) y = np.zeros((data_size,num_steps,1)) for i in range(data_size): for j in range(num_steps-prev_see): x[i,j,:] = data[i,i:i+prev_see] y[i,j,0] = data[i,i+prev_see] return x,y #@profile def main(): train_config = TrainConfig() debug_config = DebugConfig() data = np.load('processed_data.npy') x,y = process_data(data,train_config) rnn_model = RNN(True,train_config) #training phase for i in range(rnn_model.epoch): print("%dth epoch"%(i+1)) run_epoch(rnn_model,x,y) main() And following is the result of [memory_profiler](https://pypi.python.org/pypi/memory_profiler). Weird thing is, most of memory is allocated in _for loop_. (See line 163,135) I guess it means memory is leaking. Line # Mem usage Increment Line Contents ================================================ 11 53.062 MiB 0.000 MiB @profile 12 def __init__(self, is_training, config): 13 53.875 MiB 0.812 MiB self.sess = sess = tf.Session() 14 15 53.875 MiB 0.000 MiB self.prev_see = prev_see = config.prev_see 16 53.875 MiB 0.000 MiB self.num_steps = num_steps = config.num_steps 17 #maybe "self.num_hidden =" part could be removed 18 53.875 MiB 0.000 MiB self.num_hidden = num_hidden = config.num_hidden 19 53.875 MiB 0.000 MiB self.batch_size = config.batch_size 20 53.875 MiB 0.000 MiB self.epoch = config.epoch 21 53.875 MiB 0.000 MiB self.learning_rate = config.learning_rate 22 53.875 MiB 0.000 MiB self.summaries_dir = config.summaries_dir 23 24 53.875 MiB 0.000 MiB with tf.name_scope('input'): 25 53.875 MiB 0.000 MiB self.x = tf.placeholder(tf.float32,[None,num_steps,config.prev_see], 26 53.957 MiB 0.082 MiB name='input-x') 27 53.973 MiB 0.016 MiB self.y = tf.placeholder(tf.float32, [None,num_steps,1],name='input-y') 28 29 55.316 MiB 1.344 MiB def weight_variable(self,shape): 30 """Create a weight variable with appropriate initialization.""" 31 55.371 MiB 0.055 MiB initial = tf.truncated_normal(shape,stddev=0.1) 32 55.414 MiB 0.043 MiB return tf.Variable(initial) 33 34 55.707 MiB 0.293 MiB def bias_variable(self,shape): 35 """Create a bias variable with appropriate initialization.""" 36 55.727 MiB 0.020 MiB initial = tf.constant(0.1,shape=shape) 37 55.754 MiB 0.027 MiB return tf.Variable(initial) 38 39 55.754 MiB 0.000 MiB def variable_summaries(self,var,name): 40 """Attach a lot of summaries to a Tensor.""" 41 55.754 MiB 0.000 MiB with tf.name_scope('summaries'): 42 55.801 MiB 0.047 MiB mean = tf.reduce_mean(var) 43 55.824 MiB 0.023 MiB tf.scalar_summary('mean/'+name,mean) 44 55.824 MiB 0.000 MiB with tf.name_scope('stddev'): 45 55.883 MiB 0.059 MiB stddev = tf.sqrt(tf.reduce_sum(tf.square(var-mean))) 46 55.906 MiB 0.023 MiB tf.scalar_summary('stddev/'+name,stddev) 47 55.969 MiB 0.062 MiB tf.scalar_summary('max/'+name, tf.reduce_max(var)) 48 56.027 MiB 0.059 MiB tf.scalar_summary('min/'+name, tf.reduce_min(var)) 49 56.055 MiB 0.027 MiB tf.histogram_summary(name, var) 50 51 #declare weight variables as property 52 53.973 MiB -2.082 MiB layer_name = 'rnn_layer' 53 53.973 MiB 0.000 MiB with tf.name_scope(layer_name): 54 53.973 MiB 0.000 MiB with tf.name_scope('U'): 55 54.230 MiB 0.258 MiB self.U = U = weight_variable(self,[prev_see,num_hidden]) 56 54.598 MiB 0.367 MiB variable_summaries(self,U,layer_name+'/U') 57 54.598 MiB 0.000 MiB with tf.name_scope('W'): 58 54.691 MiB 0.094 MiB self.W = W = weight_variable(self,[num_hidden,num_hidden]) 59 54.961 MiB 0.270 MiB variable_summaries(self,W,layer_name+'/W') 60 54.961 MiB 0.000 MiB with tf.name_scope('b_W'): 61 55.012 MiB 0.051 MiB self.b_W = b_W = bias_variable(self,[num_hidden]) 62 55.316 MiB 0.305 MiB variable_summaries(self,b_W,layer_name+'/b_W') 63 55.316 MiB 0.000 MiB with tf.name_scope('V'): 64 55.414 MiB 0.098 MiB self.V = V = weight_variable(self,[num_hidden,1]) 65 55.707 MiB 0.293 MiB variable_summaries(self,V,layer_name+'/V') 66 55.707 MiB 0.000 MiB with tf.name_scope('b_V'): 67 55.754 MiB 0.047 MiB self.b_V = b_V = bias_variable(self,[1]) 68 56.055 MiB 0.301 MiB variable_summaries(self,b_V,layer_name+'/b_V') 69 56.055 MiB 0.000 MiB self.merged = tf.merge_all_summaries() 70 60.348 MiB 4.293 MiB self.train_writer = tf.train.SummaryWriter(config.summaries_dir,sess.graph) 71 62.496 MiB 2.148 MiB tf.initialize_all_variables().run(session=sess) Filename: rnn.py Line # Mem usage Increment Line Contents ================================================ 82 3013.336 MiB 0.000 MiB @profile 83 def inference(self): 84 """calculate outputs and loss for a single batch""" 85 3013.336 MiB 0.000 MiB total_loss = 0.0 86 3013.336 MiB 0.000 MiB outputs = [] 87 3022.352 MiB 9.016 MiB for i in range(self.batch_size): 88 3020.441 MiB -1.910 MiB state = tf.zeros([self.num_hidden]) 89 3020.441 MiB 0.000 MiB outputs.append([]) 90 3020.441 MiB 0.000 MiB loss = 0.0 91 3022.348 MiB 1.906 MiB for j in range(self.num_steps): 92 3022.285 MiB -0.062 MiB state, output = self.next_state(self.x[i,j,:],state) 93 3022.285 MiB 0.000 MiB outputs[i].append(output) 94 3022.348 MiB 0.062 MiB loss += tf.square(self.y[i,j,:]-output) 95 3022.352 MiB 0.004 MiB total_loss+=loss 96 3022.371 MiB 0.020 MiB return outputs, total_loss / (self.batch_size*self.num_steps) Filename: rnn.py Line # Mem usage Increment Line Contents ================================================ 97 3013.336 MiB 0.000 MiB @profile 98 def batch_train(self,feed_dict): 99 """train the network for a single batch""" 100 3022.371 MiB 9.035 MiB _, loss = self.inference() 101 3051.781 MiB 29.410 MiB train_step = tf.train.GradientDescentOptimizer(self.learning_rate).minimize(loss) 102 3149.891 MiB 98.109 MiB summary,loss_value, _ = self.sess.run([self.merged,loss, train_step],feed_dict=feed_dict) 103 #self.train_writer.add_summary(summary) 104 3149.891 MiB 0.000 MiB print(loss_value) Filename: rnn.py Line # Mem usage Increment Line Contents ================================================ 131 1582.758 MiB 0.000 MiB @profile 132 def run_epoch(m,x_data,y_data): 133 1582.758 MiB 0.000 MiB num_batch = ((len(x_data)-1) // m.batch_size)+1 134 #num_batch = 100 135 3149.895 MiB 1567.137 MiB for i in range(num_batch): 136 3013.336 MiB -136.559 MiB x_batch = x_data[i*m.batch_size:(i+1)*m.batch_size,:,:] 137 3013.336 MiB 0.000 MiB y_batch = y_data[i*m.batch_size:(i+1)*m.batch_size,:,:] 138 3013.336 MiB 0.000 MiB feed_dict = {m.x:x_batch,m.y:y_batch} 139 3013.336 MiB 0.000 MiB print("%dth/%dbatches"%(i+1,num_batch)) 140 3149.891 MiB 136.555 MiB m.batch_train(feed_dict) Filename: rnn.py Line # Mem usage Increment Line Contents ================================================ 154 52.914 MiB 0.000 MiB @profile 155 def main(): 156 52.914 MiB 0.000 MiB train_config = TrainConfig() 157 52.914 MiB 0.000 MiB debug_config = DebugConfig() 158 53.059 MiB 0.145 MiB data = np.load('processed_data.npy') 159 53.062 MiB 0.004 MiB x,y = process_data(data,debug_config) 160 62.496 MiB 9.434 MiB rnn_model = RNN(True,debug_config) 161 162 #training phase 163 3149.898 MiB 3087.402 MiB for i in range(rnn_model.epoch): 164 1582.758 MiB -1567.141 MiB print("%dth epoch"%(i+1)) 165 3149.898 MiB 1567.141 MiB run_epoch(rnn_model,x,y) This problem was not occured when I tried simple [MNIST model](https://www.tensorflow.org/versions/r0.9/tutorials/mnist/beginners/index.html) from tensorflow tutorial. So it should be related with RNN model. Also, I could reproduce this problem on Ubuntu 14.04, so I don't think this problem is caused by OS X things. Thank you for reading. Answer: I think the problem is that this line train_step = tf.train.GradientDescentOptimizer(self.learning_rate).minimize(loss) occurs in your batch_train function, so on every iteration, a **new** GradientDescentOptimizer is created. Try moving this to your model's init function just after you define the loss and refer to self.train_step in your batch_train function instead.
Matplotlib: scrolling plot Question: I'm new to Python and I want to implement a scrolling plot for a very long time series data. I've found an example from Matplotlib as follows. <http://scipy-cookbook.readthedocs.io/items/Matplotlib_ScrollingPlot.html> When I run the example from the link, I found every time I scroll the plot and release the scrollbar, the scrollbar returns to the beginning. Want to scroll to the next position? I need to start to scroll from the beginning again. I want to understand why it happens and how to fix it. Answer: Here's an improved version of the example. (Disclaimer: I started digging into it half an hour ago, never before used wx/matplotlib scrollbars so there might be a much better solution.) The path I took: first I checked the [wx scroll events](http://docs.wxwidgets.org/trunk/classwx_scroll_win_event.html "scroll events"), then found out that the canvas is [FigureCanvasWxAgg](http://matplotlib.org/api/backend_wxagg_api.html "FigureCanvasWxAgg") derived from wxPanel, inheriting [wxWindow](http://docs.wxwidgets.org/trunk/classwx_window.html "wxWindow") methods. There you may find the scroll position handling methods `GetScrollPos` and `SetScrollPos`. from numpy import arange, sin, pi, float, size import matplotlib matplotlib.use('WXAgg') from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg from matplotlib.figure import Figure import wx class MyFrame(wx.Frame): def __init__(self, parent, id): wx.Frame.__init__(self,parent, id, 'scrollable plot', style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER, size=(800, 400)) self.panel = wx.Panel(self, -1) self.fig = Figure((5, 4), 75) self.canvas = FigureCanvasWxAgg(self.panel, -1, self.fig) self.scroll_range = 400 self.canvas.SetScrollbar(wx.HORIZONTAL, 0, 5, self.scroll_range) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(self.canvas, -1, wx.EXPAND) self.panel.SetSizer(sizer) self.panel.Fit() self.init_data() self.init_plot() self.canvas.Bind(wx.EVT_SCROLLWIN, self.OnScrollEvt) def init_data(self): # Generate some data to plot: self.dt = 0.01 self.t = arange(0,5,self.dt) self.x = sin(2*pi*self.t) # Extents of data sequence: self.i_min = 0 self.i_max = len(self.t) # Size of plot window: self.i_window = 100 # Indices of data interval to be plotted: self.i_start = 0 self.i_end = self.i_start + self.i_window def init_plot(self): self.axes = self.fig.add_subplot(111) self.plot_data = \ self.axes.plot(self.t[self.i_start:self.i_end], self.x[self.i_start:self.i_end])[0] def draw_plot(self): # Update data in plot: self.plot_data.set_xdata(self.t[self.i_start:self.i_end]) self.plot_data.set_ydata(self.x[self.i_start:self.i_end]) # Adjust plot limits: self.axes.set_xlim((min(self.t[self.i_start:self.i_end]), max(self.t[self.i_start:self.i_end]))) self.axes.set_ylim((min(self.x[self.i_start:self.i_end]), max(self.x[self.i_start:self.i_end]))) # Redraw: self.canvas.draw() def update_scrollpos(self, new_pos): self.i_start = self.i_min + new_pos self.i_end = self.i_min + self.i_window + new_pos self.canvas.SetScrollPos(wx.HORIZONTAL, new_pos) self.draw_plot() def OnScrollEvt(self, event): evtype = event.GetEventType() if evtype == wx.EVT_SCROLLWIN_THUMBTRACK.typeId: pos = event.GetPosition() self.update_scrollpos(pos) elif evtype == wx.EVT_SCROLLWIN_LINEDOWN.typeId: pos = self.canvas.GetScrollPos(wx.HORIZONTAL) self.update_scrollpos(pos + 1) elif evtype == wx.EVT_SCROLLWIN_LINEUP.typeId: pos = self.canvas.GetScrollPos(wx.HORIZONTAL) self.update_scrollpos(pos - 1) elif evtype == wx.EVT_SCROLLWIN_PAGEUP.typeId: pos = self.canvas.GetScrollPos(wx.HORIZONTAL) self.update_scrollpos(pos - 10) elif evtype == wx.EVT_SCROLLWIN_PAGEDOWN.typeId: pos = self.canvas.GetScrollPos(wx.HORIZONTAL) self.update_scrollpos(pos + 10) else: print "unhandled scroll event, type id:", evtype class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None,id=-1) self.frame.Show() self.SetTopWindow(self.frame) return True if __name__ == '__main__': app = MyApp() app.MainLoop() You may adjust e.g. the increments for PAGEUP/PAGEDOWN if you feel it too slow. Also if you wish, the events can be handled separately setting up the specific event handlers instead of their collection `EVT_SCROLLWIN`, then instead of if/elifs there will be OnScrollPageUpEvt etc.
ValueError: Found array with dim 3. Estimator expected <= 2 Question: I'am trying to generate my own training data for recognition problem. I have two folder `s0` and `s1` and the folder containing is `data`. `images`, `lables` are the two list in which the `labels` contains the names of the folder. |—- data | |—- s0 | | |—- 1.pgm | | |—- 2.pgm | | |—- 3.pgm | | |—- 4.pgm | | |—- ... | |—- s1 | | |—- 1.pgm | | |—- 2.pgm | | |—- 3.pgm | | |—- 4.pgm | | |—- ... Below is the code, it's showing me an error on line `classifier.fit(images, lables)` Traceback (most recent call last): File "mint.py", line 34, in <module> classifier.fit(images, lables) File "/usr/local/lib/python2.7/dist-packages/sklearn/svm/base.py", line 150, in fit X = check_array(X, accept_sparse='csr', dtype=np.float64, order='C') File "/usr/local/lib/python2.7/dist- packages/sklearn/utils/validation.py", line 396, in check_array % (array.ndim, estimator_name)) ValueError: Found array with dim 3. Estimator expected <= 2. here import os,sys import cv2 import numpy as np from sklearn.svm import SVC fn_dir ='/home/aquib/Desktop/Natural/data' # Create a list of images and a list of corresponding names (images, lables, names, id) = ([], [], {}, 0) for (subdirs, dirs, files) in os.walk(fn_dir): for subdir in dirs: names[id] = subdir mypath = os.path.join(fn_dir, subdir) for item in os.listdir(mypath): if '.png' in item: label=id image = cv2.imread(os.path.join(mypath, item),0) r_image = np.resize(image,(30,30)) if image is not None: images.append(r_image) lables.append(int(label)) id += 1 #Create a Numpy array from the two lists above (images, lables) = [np.array(lis) for lis in [images, lables]] classifier = SVC(verbose=0, kernel='poly', degree=3) classifier.fit(images, lables) I really don't understand how to correct it in 2 dimension. I am trying the below codes but the error is same: ` images = np.array(images) im_sq = np.squeeze(images).shape images = images.reshape(images.shape[:2])` Answer: There is syntax error on `images.append(cv2.imread((path, 0))` last line in your code. The parenthesis are not closed properly. So it should be like this `images.append(cv2.imread((path, 0)))` . Also it is always a good thing to post the traceback for the error so that it will be easy for anyone to answer.
python threading: max number of threads to run Question: let's say i have something similar to: def worker(name): time.sleep(10) print name return thrs = [] for i in range(1000): t1 = threading.Thread(target=worker, args=(i,)) thrs.append(t1) for t in thrs: t.start() Is there way to specify how many threads can run in parallel? in the above case, all 1000 will run in parallel Answer: This can be done using [`multiprocessing.dummy`](https://docs.python.org/2/library/multiprocessing.html#module- multiprocessing.dummy) which provides a threaded version of the [`multiprocessing`](https://docs.python.org/2/library/multiprocessing.html) api. from multiprocessing.dummy import Pool pool = Pool(10) result = pool.map(worker, range(1000)) In python 3, [`concurrent.futures.ThreadPoolExecutor`](https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor) usually provides a nicer interface.
Python: clear items from PriorityQueue Question: [Clear all items from the queue ](http://stackoverflow.com/questions/6517953/clear-all-items-from-the-queue) I read the above answer Im using python 2.7 import Queue pq = Queue.PriorityQueue() pq.clear() I get the following error: AttributeError: PriorityQueue instance has no attribute 'clear' Is there way to easily empty priority queue instead of manually popping out all items? or would re-instantiating work (i.e. it wouldn't mess with join())? Answer: It's actually `pq.queue.clear()`. However, as mentioned in the answers to the question you referenced, this is not documented and potentially unsafe. The cleanest way is described in [this answer](http://stackoverflow.com/a/18873213/3165737): while not q.empty(): try: q.get(False) except Empty: continue q.task_done() Re-instantiating the queue would work too of course (the object would simple be removed from memory), as long as no other part of your code holds on to a reference to the old queue.
PyQt MainWindow closes after initialization Question: I wanted to start a new project using PyQt5 and QtDesigner. To start, I just copied the code I had from previous projects in PyQt4 and tweaked it to the changes in PyQt5. So, the code to start the Main Window and a Timer which updates the application looks like this: # ====Python============================================================= # SticksNStones # ======================================================================= import ... FPS = 45 dt = 1000.0 / FPS class SNSMainWindow(WindowBaseClass, Ui_Window): def __init__(self, parent=None): WindowBaseClass.__init__(self, parent) Ui_Window.__init__(self) self.setupUi(self) self.paused = False self.timer = None self.init() def init(self): # Setup Display self.display.setup() # Setup timer self.timer = QtCore.QTimer(self) self.timer.timeout.connect(self.update_loop) self.timer.start(self.dt) def update_loop(self): if not self.paused: self.display.update(dt) else: pass # ================================== # Start Application # ================================== _dialog = None def start_sns(): global _dialog # Start App and frame app = QtWidgets.QApplication(sys.argv) _dialog = SNSMainWindow() _dialog.show() # Exit if window is closed sys.exit(app.exec_()) if __name__ == "__main__": start_sns() But as soon as I start the application, it closes after initialization. Debugging showed that the timer is active, but the update_loop is never called. The PyQt4 Code from which I copied works just fine and I just can't get my head around why this does not work, since all examples I found online have the same code. The question being: Why does the application close itself upon start? ## Update The problem is not the timer, but the usage of a custom .ui. If I run the code with class SNSMainWindow(QtWidgets.QFrame): def __init__(self, parent=None): QtWidgets.QFrame.__init__(self, parent) ... a window opens and it stays open until I close it. But a barebone ui_path = os.path.dirname(os.path.abspath(__file__)) + "/ui/sns_main.ui" Ui_Window, WindowBaseClass = uic.loadUiType(ui_path) class SNSMainWindow(WindowBaseClass, Ui_Window): def __init__(self, parent=None): WindowBaseClass.__init__(self, parent) Ui_Window.__init__(self) self.setupUi(self) # ================================== if __name__ == "__main__": # Start App and frame app = QtWidgets.QApplication(sys.argv) _dialog = SNSMainWindow() _dialog.show() # Exit if window is closed sys.exit(app.exec_()) just disappears within milliseconds after showing. Then again, using the custom widget in PyQt4 stays open, too. I added the uic.load part, which operates just fine. Am I missing something when converting to PyQt5? ## Solution I found the solution of the problem in my custom display class. In case of a paintEvent, the display would try to get a (yet) undefined property. But instead of raising an exception that the property was not defined, the window just closed. Defining the property while initializing the widget solved the problem. This just keeps me wondering, why no exceptions are raised in this case, since the widget clearly tries to call some undefined properties. A simple AttributeError: 'NoneType' object has no attribute 'xxx' would have been enough. Answer: I'd try to change some lines, first try to change `app` definition to app = QtGui.QApplication(sys.argv) Then remove `Ui_Window` init and set it to `self.ui = Ui_Window()` class SNSMainWindow(WindowBaseClass): def __init__(self, parent=None): WindowBaseClass.__init__(self, parent) self.ui = Ui_Window() self.ui.setupUi(self) self.paused = False self.timer = None self.init()
OpenCV + Python - NameError: name 'imSize' is not defined Question: I am using `OpenCV` for image processing. In which, I am getting these errors please suggest what to do, below is my code: import dicom import Image import ImageOps meta=dicom.read_file("E:\A_SHIVA\ANANADAN\IM_0.dcm") imHeight=meta.Rows imWidth=meta.Columns imSize=(imWidth,imHeight) TT=Image.frombuffer("L",imSize,meta.PixelData,"raw","L",0,1) TT.save("testOUTPUT.tiff","TIFF",compression="none") The error is below: Traceback (most recent call last): File "C:\Users\sairamsystem\AppData\Local\Enthought\Canopy\User\lib\site-packages\IPython\core\interactiveshell.py", line 3066, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-8-640c37dc4648>", line 1, in <module> TT=Image.frombuffer('L',imSize,meta.PixelData,"raw","L",0,1) NameError: name 'imSize' is not defined Answer: The code is fine. It worked for me. The name error exception happened because it could not find imSize variable, which means it never got made. This can only happen when you haven't read the Dicom file properly, so try changing the path and printing the variable 'meta' to see if it has any value. After reading the file also print and check if meta.Rows and meta.Columns are having the correct values or not.
Regex replacement for strip() Question: Long time/first time. I am a pharmacist by trade by am going through the motions of teaching myself how to code in a variety of languages that are useful to me for things like task automation at work, but mainly Python 3.x. I am working through the automatetheboringstuff eBook and finding it great. I am trying to complete one of the practice questions from Chapter 7: _"Write a function that takes a string and does the same thing as the strip() string method. If no other arguments are passed other than the string to strip, then whitespace characters will be removed from the beginning and end of the string. Otherwise, the characters specified in the second argument to the function will be removed from the string."_ I am stuck for the situation when the characters I want to strip appear in the string I want to strip them from e.g. 'ssstestsss'.strip(s) #!python3 import re respecchar = ['?', '*', '+', '{', '}', '.', '\\', '^', '$', '[', ']'] def regexstrip(string, _strip): if _strip == '' or _strip == ' ': _strip = r'\s' elif _strip in respecchar: _strip = r'\'+_strip' print(_strip) #just for troubleshooting re_strip = re.compile('^'+_strip+'*(.+)'+_strip+'*$') print(re_strip) #just for troubleshooting mstring = re_strip.search(string) print(mstring) #just for troubleshooting stripped = mstring.group(1) print(stripped) As it is shown, running it on ('ssstestsss', 's') will yield 'testsss' as the .+ gets all of it and the * lets it ignore the final 'sss'. If I change the final * to a + it only improves a bit to yield 'testss'. If I make the capture group non-greedy (i.e. (.+)? ) I still get 'testsss' and if exclude the character to be stripped from the character class for the capture group and remove the end string anchor (i.e. `re.compile('^'+_strip+'*([^'+_strip+'.]+)'+_strip+'*')` I get 'te' and if I don't remove the end string anchor then it obviously errors. Apologies for the verbose and ramble-y question. I deliberately included all the code (work in progress) as I am only learning so I realise that my code is probably rather inefficient, so if you can see any other areas where I can improve my code, please let me know. I know that there is no practical application for this code, but I'm going through this as a learning exercise. I hope I have asked this question appropriately and haven't missed anything in my searches. Regards Lobsta Answer: You `(.+)` is greedy, (by default). Just change it to non greedy, by using `(.+?)` You can test python regex at [this site](https://regex101.com/#python) edit : As someone commented, `(.+?)` and `(.+)?` do not do the same thing : `(.+?)` is the non greedy version of `(.+)` while `(.+)?` matches or not the greedy `(.+)`
django-debug-toolbar: 'Template' object has no attribute 'engine' Question: I've just tried running an existing Django project on a new computer, and I'm having trouble with django-debug-toolbar. It seems to be something to do with Jinja2. Here's the stack trace: Traceback: File "/path/to/myrepo/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 223. response = middleware_method(request, response) File "/path/to/myrepo/env/local/lib/python2.7/site-packages/debug_toolbar/middleware.py" in process_response 120. panel.generate_stats(request, response) File "/path/to/myrepo/env/local/lib/python2.7/site-packages/debug_toolbar/panels/templates/panel.py" in generate_stats 175. context_processors = self.templates[0]['context_processors'] Exception Type: AttributeError at /first/page/ Exception Value: 'Template' object has no attribute 'engine' I'm using django-jinja2 to integrate Jinja2 into my project, and this worked okay before but it now seems to be expecting this `template` variable to be a normal Django template. In my `TEMPLATES` setting I have both Jinja2 and DjangoTemplates set up, with Jinja2 using a specific extension ('tmpl') to make sure only those templates are used by Jinja2 and everything else can go through the DjangoTemplates backend. Has anyone seen this error before when using django debug toolbar with Jinja2? I can post more settings if needed. EDIT: As requested, here's my TEMPLATES settings: TEMPLATES = [ { #'BACKEND': 'django.template.backends.jinja2.Jinja2', 'BACKEND': 'django_jinja.backend.Jinja2', #'NAME': 'jinja2', 'DIRS': [ os.path.join(DEPLOY_PATH, 'templates') ], 'APP_DIRS': True, 'OPTIONS': { 'debug': DEBUG, 'match_extension': '.tmpl', #'environment': 'jinja2.Environment', 'extensions': [ 'jinja2.ext.with_', 'jinja2.ext.i18n', 'django_jinja.builtins.extensions.UrlsExtension', 'django_jinja.builtins.extensions.CsrfExtension', 'pipeline.templatetags.ext.PipelineExtension', ], 'context_processors': [ "django.contrib.auth.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n", "django.core.context_processors.media", "django.core.context_processors.static", "django.contrib.messages.context_processors.messages", "django.core.context_processors.request", ] }, }, { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [ os.path.join(DEPLOY_PATH, 'templates') ], 'APP_DIRS': True, 'OPTIONS': { 'debug': DEBUG, 'context_processors': [ "django.contrib.auth.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n", "django.core.context_processors.media", "django.core.context_processors.static", "django.contrib.messages.context_processors.messages", "django.core.context_processors.request", ] } } ] **Update** \- I've "fixed" the issue by making a small code change in the debug toolbar source: changing line 175 in `debug_toolbar/panels/templates/panel.py` from: template_dirs = self.templates[0]['template'].engine.dirs to: if hasattr(self.templates[0]['template'], 'engine'): template_dirs = self.templates[0]['template'].engine.dirs elif hasattr(self.templates[0]['template'], 'backend'): template_dirs = self.templates[0]['template'].backend.dirs else: raise RuntimeError("Couldn't find engine or backend for a template: {}",format(self.templates[0]['template'])) I haven't looked into why this works (for some people this combination of debug toolbar 1.5 and django-jinja 2.2.0 works fine) but I noticed that Jinja2 templates have the `backend` attribute and the Django templates have the `engine` attribute, and both appear to be used for the same thing. Answer: I get this too, you can hack it fixed based on your suggestion without hacking core by supplying your own panel class: **debug.py** from debug_toolbar.panels.templates import TemplatesPanel as BaseTemplatesPanel class TemplatesPanel(BaseTemplatesPanel): def generate_stats(self, *args): template = self.templates[0]['template'] if not hasattr(template, 'engine') and hasattr(template, 'backend'): template.engine = template.backend return super().generate_stats(*args) **settings.py** DEBUG_TOOLBAR_PANELS = [ 'debug_toolbar.panels.versions.VersionsPanel', 'debug_toolbar.panels.timer.TimerPanel', 'debug_toolbar.panels.settings.SettingsPanel', 'debug_toolbar.panels.headers.HeadersPanel', 'debug_toolbar.panels.request.RequestPanel', 'debug_toolbar.panels.sql.SQLPanel', 'debug_toolbar.panels.staticfiles.StaticFilesPanel', 'myapp.debug.TemplatesPanel', # original broken by django-jinja, remove this whole block later 'debug_toolbar.panels.cache.CachePanel', 'debug_toolbar.panels.signals.SignalsPanel', 'debug_toolbar.panels.logging.LoggingPanel', 'debug_toolbar.panels.redirects.RedirectsPanel', ]
Append and Truncating together in Python Question: So, I am at exercise 16 of Zed Shaw's Python book. I thought of trying out both append and truncate on the same file. I know, this does not make sense. But, I am new and I am trying to learn what would happen if I used both. So, first I am opening the file in append mode and then truncating it and then writing into it. But, the truncate is not working here and whatever I write gets appended to the file. So, can someone kindly explain why the truncate would not work ? Even though I am opening the file in append mode first, I believe I am calling the truncate function after that and it should have worked !!! Following is my code: from sys import argv script, filename = argv print "We're going to erase %r." %filename print "If you don't want that. hit CTRL-C (^C)." print "If you do want that, hit RETURN." raw_input("?") print "Opening the file..." target = open(filename, 'a') print "Truncating the file. Goodbye!" target.truncate() print "Now I'm going to ask you for three lines." line1 = raw_input("line 1: ") line2 = raw_input("line 2: ") line3 = raw_input("line 3: ") print "I'm going to write these to the file." target.write(line1 + "\n" + line2 + "\n" + line3) print "And finally, we close it." target.close() Answer: The argument `'a'` opens the file for appending. You will need to use `'w'` instead.
Scraping Table With Python/BS4 Question: Im trying to scrape the "Team Stats" table from <http://www.pro-football- reference.com/boxscores/201602070den.htm> with BS4 and Python 2.7. However Im unable to get anywhere close to it, url = 'http://www.pro-football-reference.com/boxscores/201602070den.htm' page = requests.get(url) soup = BeautifulSoup(page.text, "html5lib") table=soup.findAll('table', {'id':"team_stats", "class":"stats_table"}) print table I thought something like the above code would work but no luck. Answer: The problem in this case is that the _"Team Stats" table is located inside a comment_ in the HTML source which you download with `requests`. Locate the comment and reparse it with `BeautifulSoup` into a "soup" object: import requests from bs4 import BeautifulSoup, NavigableString url = 'http://www.pro-football-reference.com/boxscores/201602070den.htm' page = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'}) soup = BeautifulSoup(page.content, "html5lib") comment = soup.find(text=lambda x: isinstance(x, NavigableString) and "team_stats" in x) soup = BeautifulSoup(comment, "html5lib") table = soup.find("table", id="team_stats") print(table) And/or, you can load the table into, for example, a [`pandas` dataframe](http://pandas.pydata.org/) which is very convenient to work with: import pandas as pd import requests from bs4 import BeautifulSoup from bs4 import NavigableString url = 'http://www.pro-football-reference.com/boxscores/201602070den.htm' page = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'}) soup = BeautifulSoup(page.content, "html5lib") comment = soup.find(text=lambda x: isinstance(x, NavigableString) and "team_stats" in x) df = pd.read_html(comment)[0] print(df) Prints: Unnamed: 0 DEN CAR 0 First Downs 11 21 1 Rush-Yds-TDs 28-90-1 27-118-1 2 Cmp-Att-Yd-TD-INT 13-23-141-0-1 18-41-265-0-1 3 Sacked-Yards 5-37 7-68 4 Net Pass Yards 104 197 5 Total Yards 194 315 6 Fumbles-Lost 3-1 4-3 7 Turnovers 2 4 8 Penalties-Yards 6-51 12-102 9 Third Down Conv. 1-14 3-15 10 Fourth Down Conv. 0-0 0-0 11 Time of Possession 27:13 32:47
What is the proper way to import large amounts data into a Firebase database? Question: I'm working with a dataset of political campaign contributions that ends up being an approximately 500mb JSON file (originally a 124mb CSV). It's far too big to import in the Firebase web interface (trying before crashed the tab on Google Chrome). I attempted manually uploading objects as they were made from the CSV (Using a CSVtoJSON converter, each row becomes a JSON object, and I would then upload that object to Firebase as they came). Here's the code I used. var firebase = require('firebase'); var Converter = require("csvtojson").Converter; firebase.initializeApp({ serviceAccount: "./credentials.json", databaseURL: "url went here" }); var converter = new Converter({ constructResult:false, workerNum:4 }); var db = firebase.database(); var ref = db.ref("/"); var lastindex = 0; var count = 0; var section = 0; var sectionRef; converter.on("record_parsed",function(resultRow,rawRow,rowIndex){ if (rowIndex >= 0) { sectionRef = ref.child("reports" + section); var reportRef = sectionRef.child(resultRow.Report_ID); reportRef.set(resultRow); console.log("Report uploaded, count at " + count + ", section at " + section); count += 1; lastindex = rowIndex; if (count >= 1000) { count = 0; section += 1; } if (section >= 100) { console.log("last completed index: " + lastindex); process.exit(); } } else { console.log("we out of indices"); process.exit(); } }); var readStream=require("fs").createReadStream("./vUPLOAD_MASTER.csv"); readStream.pipe(converter); However, that ran into memory issues and wasn't able to complete the dataset. Trying to do it in chunks was not viable either as Firebase wasn't showing all the data uploaded and I wasn't sure where I left off. (When leaving the Firebase database open in Chrome, I would see data coming in, but eventually the tab would crash and upon reloading a lot of the later data was missing.) ~~I then tried using[Firebase Streaming Import](https://github.com/firebase/firebase-streaming-import), however that throws this error: started at 1469471482.77 Traceback (most recent call last): File "import.py", line 90, in <module> main(argParser.parse_args()) File "import.py", line 20, in main for prefix, event, value in parser: File "R:\Python27\lib\site-packages\ijson\common.py", line 65, in parse for event, value in basic_events: File "R:\Python27\lib\site-packages\ijson\backends\python.py", line 185, in basic_parse for value in parse_value(lexer): File "R:\Python27\lib\site-packages\ijson\backends\python.py", line 127, in parse_value raise UnexpectedSymbol(symbol, pos) ijson.backends.python.UnexpectedSymbol: Unexpected symbol u'\ufeff' at 0 Looking up that last line (the error from ijson), I found [this SO thread](https://stackoverflow.com/questions/17912307/u-ufeff-in-python- string), but I'm just not sure how I'm supposed to use that to get Firebase Streaming Import working.~~ I removed the Byte Order Mark using Vim from the JSON file I was trying to upload, and now I get this error after a minute or so of running the importer: Traceback (most recent call last): File "import.py", line 90, in <module> main(argParser.parse_args()) File "import.py", line 20, in main for prefix, event, value in parser: File "R:\Python27\lib\site-packages\ijson\common.py", line 65, in parse for event, value in basic_events: File "R:\Python27\lib\site-packages\ijson\backends\python.py", line 185, in basic_parse for value in parse_value(lexer): File "R:\Python27\lib\site-packages\ijson\backends\python.py", line 116, in parse_value for event in parse_array(lexer): File "R:\Python27\lib\site-packages\ijson\backends\python.py", line 138, in parse_array for event in parse_value(lexer, symbol, pos): File "R:\Python27\lib\site-packages\ijson\backends\python.py", line 119, in parse_value for event in parse_object(lexer): File "R:\Python27\lib\site-packages\ijson\backends\python.py", line 170, in parse_object pos, symbol = next(lexer) File "R:\Python27\lib\site-packages\ijson\backends\python.py", line 51, in Lexer buf += data MemoryError The Firebase Streaming Importer is supposed to be able to handle files upwards of 250mb, and I'm fairly certain I have more than enough RAM to handle this file. Any ideas as to why this error is appearing? If seeing the actual JSON file I'm trying to upload with Firebase Streaming Import would help, [here it is](https://dl.dropboxusercontent.com/u/75535527/json_file.zip). Answer: I got around the problem by giving up on the Firebase Streaming Import and writing my own tool that used csvtojson to convert the CSV and then the Firebase Node API to upload each object one at a time. Here's the script: var firebase = require("firebase"); firebase.initializeApp({ serviceAccount: "./credentials.json", databaseURL: "https://necir-hackathon.firebaseio.com/" }); var db = firebase.database(); var ref = db.ref("/reports"); var fs = require('fs'); var Converter = require("csvtojson").Converter; var header = "Report_ID,Status,CPF_ID,Filing_ID,Report_Type_ID,Report_Type_Description,Amendment,Amendment_Reason,Amendment_To_Report_ID,Amended_By_Report_ID,Filing_Date,Reporting_Period,Report_Year,Beginning_Date,Ending_Date,Beginning_Balance,Receipts,Subtotal,Expenditures,Ending_Balance,Inkinds,Receipts_Unitemized,Receipts_Itemized,Expenditures_Unitemized,Expenditures_Itemized,Inkinds_Unitemized,Inkinds_Itemized,Liabilities,Savings_Total,Report_Month,UI,Reimbursee,Candidate_First_Name,Candidate_Last_Name,Full_Name,Full_Name_Reverse,Bank_Name,District_Code,Office,District,Comm_Name,Report_Candidate_First_Name,Report_Candidate_Last_Name,Report_Office_District,Report_Comm_Name,Report_Bank_Name,Report_Candidate_Address,Report_Candidate_City,Report_Candidate_State,Report_Candidate_Zip,Report_Treasurer_First_Name,Report_Treasurer_Last_Name,Report_Comm_Address,Report_Comm_City,Report_Comm_State,Report_Comm_Zip,Category,Candidate_Clarification,Rec_Count,Exp_Count,Inkind_Count,Liab_Count,R1_Count,CPF9_Count,SV1_Count,Asset_Count,Savings_Account_Count,R1_Item_Count,CPF9_Item_Count,SV1_Item_Count,Filing_Mechanism,Also_Dissolution,Segregated_Account_Type,Municipality_Code,Current_Report_ID,Location,Individual_Or_Organization,Notable_Contributor,Currently_Accessed" var queue = []; var count = 0; var upload_lock = false; var lineReader = require('readline').createInterface({ input: fs.createReadStream('test.csv') }); lineReader.on('line', function (line) { var line = line.replace(/'/g, "\\'"); var csvString = header + '\n' + line; var converter = new Converter({}); converter.fromString(csvString, function(err,result){ if (err) { var errstring = err + "\n"; fs.appendFile('converter_error_log.txt', errstring, function(err){ if (err) { console.log("Converter: Append Log File Error Below:"); console.error(err); process.exit(1); } else { console.log("Converter Error Saved"); } }); } else { result[0].Location = ""; result[0].Individual_Or_Organization = ""; result[0].Notable_Contributor = ""; result[0].Currently_Accessed = ""; var reportRef = ref.child(result[0].Report_ID); count += 1; reportRef.set(result[0]); console.log("Sent #" + count); } }); }); The only caveat is although the script can quickly send out all the objects, Firebase apparently needed the connection to remain while it was saving them, as closing the script after all objects were sent resulted in a lot of objects not appearing in the database. (I waited 20 minutes to be sure, but it might be shorter)
Error compiling C code for python hmmlearn package Question: I'm having some trouble getting the `hmmlearn` package to install properly (in a virtual environment); it seems to have something to do with the underlying C code. The package installs fine with `pip`, but when I try to import the core class, I get an error: In [1]: import hmmlearn In [2]: from hmmlearn import hmm --------------------------------------------------------------------------- ImportError Traceback (most recent call last) <ipython-input-2-8b8c029fb053> in <module>() ----> 1 from hmmlearn import hmm /export/hdi3/home/krono/envs/sd/lib/python2.7/site-packages/hmmlearn/hmm.py in <module>() 19 from sklearn.utils import check_random_state 20 ---> 21 from .base import _BaseHMM 22 from .utils import iter_from_X_lengths, normalize 23 /export/hdi3/home/krono/envs/sd/lib/python2.7/site-packages/hmmlearn/base.py in <module>() 11 from sklearn.utils.validation import check_is_fitted 12 ---> 13 from . import _hmmc 14 from .utils import normalize, log_normalize, iter_from_X_lengths 15 ImportError: /export/hdi3/home/krono/envs/sd/lib/python2.7/site-packages/hmmlearn/_hmmc.so: undefined symbol: npy_expl I've been reading other questions on SO which seem to treat this, but [one solution](http://stackoverflow.com/questions/30428278/undefined-symbols-in- scipy-and-scikit-learn-on-redhat) (use Anaconda) won't work since `hmmlearn` isn't included. It seems like the answer has something to do with compiling the C code, but I'm not sure how to go about it. Any help would be much appreciated! Answer: I ran into the same issue a while ago and found the [solution](http://stackoverflow.com/a/35123700/3846213) by trying everything possible. For whatever reason in some cases `pip` skips building C-extensions, when a package is saved into the cache directory. If you force `pip` to ignore the cache, it always builds the package from scratch, so the solution is to uninstall the package in the first place and then run `pip install --no-cache- dir <package>`
How to import a class from a python file in django? Question: I'm currently using django and putting python code in my views.py to run them on my webpages. There is an excerpt of code that requires a certain class from a python file to work sort of like a package but it is just a python file I was given to use in order to execute a certain piece of code. How would I be able to reference the class from my python file in the django views.py file? I have tried putting the python file in my site packages folder in my Anaconda3 folder and have tried just using from [name of python file] import [class name] in the views.py file but it does not seem to recognize that the file exists in the site packages folder. I also tried putting the python file in the django personal folder and using from personal import [name of file] but that doesn't work either Answer: You can simply put the `mypythonfile.py` in the same directory of your `views.py` file. And `from mypythonfile import mystuff` in your views.py
How to fix "unexpected keyword argument 'useChardet'" in html5lib Question: I'm using html5lib and after updating it to the latest version, I keep getting this error: Traceback (most recent call last): File "/home/travis/build/freelawproject/juriscraper/tests/test_everything.py", line 119, in test_scrape_all_example_files site.parse() File "/home/travis/build/freelawproject/juriscraper/juriscraper/AbstractSite.py", line 95, in parse self.html = self._download() File "/home/travis/build/freelawproject/juriscraper/juriscraper/AbstractSite.py", line 384, in _download html_tree = self._make_html_tree(text) File "/home/travis/build/freelawproject/juriscraper/juriscraper/opinions/united_states/federal_appellate/ca11_u.py", line 26, in _make_html_tree e = html5parser.document_fromstring(text) File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/lxml/html/html5parser.py", line 64, in document_fromstring return parser.parse(html, useChardet=guess_charset).getroot() File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/html5lib/html5parser.py", line 235, in parse self._parse(stream, False, None, *args, **kwargs) File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/html5lib/html5parser.py", line 85, in _parse self.tokenizer = _tokenizer.HTMLTokenizer(stream, parser=self, **kwargs) File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/html5lib/_tokenizer.py", line 36, in __init__ self.stream = HTMLInputStream(stream, **kwargs) File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/html5lib/_inputstream.py", line 149, in HTMLInputStream return HTMLUnicodeInputStream(source, **kwargs) TypeError: __init__() got an unexpected keyword argument 'useChardet' The code I'm using is very simple: from lxml.html import html5parser html5parser.document_fromstring(u'<html></html') Any ideas? Answer: Turns out that if you feed a unicode object to the `document_fromstring` method, it barfs. It didn't used to because this only happened when I updated my dependencies. Anyway, the fix is easy: html5parser.document_fromstring(u'<html></html'.encode('utf-8'))
How to show image from webcam Question: I never needed to work with camera before, so I do not know where to start. I need to display the image of a real-time camera, to capture and save to a file. I'm using python3 and gtk3. Does gtk has any feature for this? Answer: The easiest way to get the image from a webcam is to use OpenCV. It allows you to get the image with just 2 lines of code and 2 more to show it, like so: import numpy as np import cv2 cap = cv2.VideoCapture(0) ret, frame = cap.read() cv2.imshow('frame',frame) cv2.waitKey(0) But there is a downside, namely that OpenCV for Python 3 has to be build from source. Most people don't like building from source so they say it is not available. Luckily there is the [Unofficial Windows Binaries for Python Extension Packages by Gohlke (University of California)](http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv) which also offers a precompiled version of OpenCV for Python 3. Installing that using `pip` should be easy.
KIVY - Python Keep doing while button is pressed Question: A few days ago (yesterday actually :D) I started programming in KIVY (which is a python module -- if I can call it that). I'm pretty familiar with python itself but I find KIVY pretty problamatic and difficult in some solutions. I'm currently trying to make a platformer game for IOS/Android but I'm stuck on a problem. I've created two buttons and a character. I want the character to keep moving until the button is released. By that I mean: I can move the character once, when the button is pressed, but I want it to keep moving until the button is released. I've tried multiple solution, for example I used pythons time module: class Level1(Screen): posx = NumericProperty(0) posy = NumericProperty(0) moving = True i = 0 def __init__(self, **kwargs): super(Level1, self).__init__(**kwargs) def rightmove(self): self.posx = self.posx+1 time.sleep(10) def goright(self): while self.moving == True: self.rightmove() i += 1 if i == 10: break def stopright(self): self.moving == False but it doesn't work. It think that it somehow is put in an endless loop, because when I press the button the app stop working ("app stopped working..." error). I have pretty much no idea how I can fix this. I've been trying for the last few hours and havn't found a solution yet. Here's my .py file: from kivy.app import App from kivy.lang import Builder from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition, SlideTransition from kivy.config import Config from kivy.core.window import Window from kivy.uix.label import Label from kivy.uix.image import Image from kivy.uix.widget import Widget from kivy.properties import ObjectProperty, NumericProperty from kivy.clock import Clock from kivy.uix.floatlayout import FloatLayout import time Config.set('graphics','resizable',0) #don't make the app re-sizeable #Graphics fix #this fixes drawing issues on some phones Window.clearcolor = (0,0,0,1.) language = "english" curr1msg = 1 class HomeScreen(Screen): pass class OptionsScreen(Screen): pass class GameScreen(Screen): pass class LevelScreen(Screen): pass class Level1intro(Screen): global language global curr1msg if language == "english" and curr1msg == 1: pName = "Pedro" msg1 = """Hello my friend! My name is Pedro and I have a problem. Will you help me? My spanish studens have a spanish test tomorrow, but I lost the exams! You are the only one who can help me!""" cont = "Press anywhere to continue..." elif language == "swedish" and curr1msg == 1: pName = "Pedro" msg1 = """Hejsan! Jag är Pedro och jag har ett problem. Kan du hjälpa mig? Mina spanska-elever har ett spanskaprov imorgon men jag har tappat bort proven! Du är den enda som kan hjälpa mig!""" cont = "Tryck på skärmen för att fortsätta..." class Level1(Screen): posx = NumericProperty(0) posy = NumericProperty(0) moving = True i = 0 def __init__(self, **kwargs): super(Level1, self).__init__(**kwargs) def rightmove(self): self.posx = self.posx+1 time.sleep(10) def goright(self): while self.moving == True: self.rightmove() i += 1 if i == 10: break def stopright(self): self.moving == False class ScreenManagement(ScreenManager): pass presentation = Builder.load_file("main.kv") class MainApp(App): def build(self): return presentation if __name__ == "__main__": MainApp().run() And here is my .kv file: #: import FadeTransition kivy.uix.screenmanager.FadeTransition #: import SlideTransition kivy.uix.screenmanager.SlideTransition ScreenManagement: transition: FadeTransition() HomeScreen: OptionsScreen: LevelScreen: Level1intro: Level1: <HomeScreen>: name: 'home' FloatLayout: canvas: Rectangle: source:"images/home_background.jpg" size: self.size Image: source:"images/logo.png" allow_stretch: False keep_ratio: False opacity: 1.0 size_hint: 0.7, 0.8 pos_hint: {'center_x': 0.5, 'center_y': 0.9} Button: size_hint: 0.32,0.32 pos_hint: {"x":0.34, "y":0.4} on_press: app.root.transition = SlideTransition(direction="left") app.root.current = "level" background_normal: "images/play_button.png" allow_stretch: False Button: size_hint: 0.25,0.25 pos_hint: {"x":0.38, "y":0.15} on_press: app.root.transition = SlideTransition(direction="left") app.root.current = 'options' background_normal: "images/settings_button.png" <OptionsScreen>: name: 'options' <LevelScreen> name: "level" FloatLayout: canvas: Rectangle: source:"images/home_background.jpg" size: self.size Label: text: "[b]Choose Level[/b]" markup: 1 font_size: 40 color: 1,0.5,0,1 pos: 0,250 Button: size_hint: 0.1,0.1 pos_hint: {"x": 0.1, "y": 0.8} on_press: app.root.current = "level1intro" Image: source:"images/level1.png" allow_stretch: True y: self.parent.y + self.parent.height - 70 x: self.parent.x height: 80 width: 80 Button: background_normal: "images/menu_button.png" pos_hint: {"x": 0.4, "y": 0} size_hint: 0.3,0.3 pos_hint: {"x": 0.35} on_press: app.root.transition = SlideTransition(direction="right") app.root.current = "home" <Level1intro> name: "level1intro" canvas: Rectangle: source: "images/background.png" size: self.size Image: source: "images/dialog.png" pos_hint: {"y": -0.35} size_hint: 0.7,1.0 Label: font_size: 20 color: 1,1,1,1 pos_hint: {"x": -0.385, "y": -0.285} text: root.pName Label: font_size: 15 color: 1,1,1,1 pos_hint: {"x": -0.15, "y": -0.4} text: root.msg1 Label: font_size: 15 color: 0.7,0.8,1,1 pos_hint: {"x": 0.025, "y": -0.449} text: root.cont on_touch_down: app.root.transition = FadeTransition() app.root.current = "level1" <Level1> name: "level1" canvas: Rectangle: source: "images/background.png" size: self.size Button: text: ">" size_hint: 0.1,0.1 pos_hint: {"x":0.9, "y":0.0} on_press: root.goright() on_release: root.stopright() Button: text: "<" size_hint: 0.1,0.1 pos_hint: {"x": 0.0, "y": 0.0} on_press: root.posx = root.posx-1 Image: id: char source: "images/idle1.png" size: self.size pos: root.posx,root.posy Thank you for your time and help. GryTrean //I changed "i" to "self.i" and it doesn't fix the problem. Answer: [Here](https://kivy.org/docs/api- kivy.uix.behaviors.button.html#kivy.uix.behaviors.button.ButtonBehavior) is the button API in kivy. The two bindings that applicable to your problem are the `on_press` and `on_release` bindings. You would use these with the `Button.bind()` method. An example of binding a function to button binding is available [here](https://kivy.org/docs/api-kivy.uix.button.html#).
Invalid ELF header tensorflow Question: I first tried installing tensorflow via the following: user@WS1:~/July 2016$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl user@WS1:~/July 2016$ pip install --upgrade $TF_BINARY_URL Then I tried using a (slightly modified version for linux and tensorflow 0.9.0) solution from [iRapha here](https://github.com/tensorflow/tensorflow/issues/135): user@WS1:~/July 2016$ wget https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl user@WS1:~/July 2016$ pip install tensorflow-0.9.0-cp27-none-linux_x86_64.whl Then I tried to test whether tensorflow was successfully installed. The following output shows that there is an 'invalid ELF header' error. user@WS1:~/July 2016$ python Python 2.7.12 |Anaconda 2.5.0 (64-bit)| (default, Jul 2 2016, 17:42:40) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2 >>> import tensorflow Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/export/mlrg/caugusta/anaconda2/lib/python2.7/site-packages /tensorflow/__init__.py", line 23, in <module> from tensorflow.python import * File "/export/mlrg/caugusta/anaconda2/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 48, in <module> from tensorflow.python import pywrap_tensorflow File "/export/mlrg/caugusta/anaconda2/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module> _pywrap_tensorflow = swig_import_helper() File "/export/mlrg/caugusta/anaconda2/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 24, in swig_import_helper _mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description) ImportError: /export/mlrg/caugusta/anaconda2/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so: invalid ELF header I've checked [here](http://stackoverflow.com/questions/5713731/what-does-this- error-mean-invalid-elf-header). Based on that answer, I tried: user@WS1:~July 2016$ pip install tensorflow Everything says that tensorflow installed successfully, but when I import it in python I get that invalid ELF header error. Anyone know how I can resolve this? Answer: This could happen if you have install tensorflow package which does not match your platform. An extreme example would be installing the mac platform package (as seen below) on linux. export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0rc0-cp35-cp35m-linux_x86_64.whl Just make sure you use the correct platform.
Python 3 Dice Sim issue Question: I am BRAND new to python because my new school requires it. I am used to c++ so I'm still learning the ropes. I'm trying to make a dice rolling simulator and I thought i was doing everything right but my code just won't work. Any tips or guides to help me learn would be greatly appreciated. Here is my code: import random def roll(sides=6): num_rolled = random.randint(l,sides) return num_rolled def main(): sides = 6 rolling = True while rolling: roll_again = input("Ready to roll? ENTER=Roll. Q=Quit. ") if roll_again.lower() != "q": num_rolled = roll(sides) print("You rolled a", num_rolled) else: rolling = False print("Thanks for playing!") main() This is the error I get: > Traceback (most recent call last): File > "C:\Users\nomor\AppData\Local\Programs\Python\Python35-32\DiceRollingSim.py", > line 20, in main() File > "C:\Users\nomor\AppData\Local\Programs\Python\Python35-32\DiceRollingSim.py", > line 13, in main num_rolled = roll(sides) File > "C:\Users\nomor\AppData\Local\Programs\Python\Python35-32\DiceRollingSim.py", > line 4, in roll num_rolled = random.randint(l,sides) NameError: name 'l' is > not defined Answer: For the first issue... num_rolled = random.randint(l,sides) `l != 1`. You put an "l" instead of the number 1. Python thinks this is a variable which you haven't defined anywhere -> the error you get. From the [documentation](https://docs.python.org/3/library/random.html) randint takes two ints as parameters: > random.randint(a, b) Return a random integer N such that a <= N <= b. Alias > for randrange(a, b+1). Next, look at these lines in your code. while rolling: roll_again = input("Ready to roll? ENTER=Roll. Q=Quit. ") if roll_again.lower() != "q": num_rolled = roll(sides) print("You rolled a", num_rolled) else: rolling = False The `if-else` part needs to be indented inside of the while loop: while rolling: roll_again = input("Ready to roll? ENTER=Roll. Q=Quit. ") if roll_again.lower() != "q": num_rolled = roll(sides) print("You rolled a", num_rolled) else: rolling = False
How to Save file names and their directories path in a text file using Python Question: I am trying to find a string that is contained in files under a directory. Then make it to store it's file names and directories under a new text file or something. I got upto where it is going through a directory and finding a string, then printing a result. But not sure of the next step. Please help, I'm completely new to coding and python. import glob, os #Open a source as a file and assign it as source source = open('target.txt').read() filedirectories = [] #locating the source file and printing the directories. os.chdir("/Users/a1003584/desktop") for root, dirs, files in os.walk(".", topdown=True): for name in files: print(os.path.join(root, name)) if source in open(os.path.join(root, name)).read(): print 'treasure found.' Answer: Don't do a string comparison if your looking for a dictionary. Instead use the json module. Like this. import json import os filesFound = [] def searchDir(dirName): for name in os.listdir(dirName): # If it is a file. if os.isfile(dirName+name): try: fileCon = json.load(dirName+name) except: print("None json file.") if "KeySearchedFor" in fileCon: filesFound.append(dirName+name) # If it is a directory. else: searchDir(dirName+name+'/') # Change this to the directory your looking in. searchDir("~/Desktop") open("~/Desktop/OutFile.txt",'w').write(filesFound)
gaierror: [Errno 8] nodename nor servname provided, or not known Question: Hello I am new to python , I am writing a code which generates dns requests from socket import error as socket_error import threading from random import randint from time import sleep def task(number): try : HOST = Random_website.random_website().rstrip() # fetches url PORT = 80 # The same port as used by the server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) print(str(number) +":"+HOST +"Connected") except socket_error as serr: if serr.errno != errno.ECONNREFUSED: # Not the error we are looking for, re-raise raise serr thread_list = [] for i in range(1, 100): t = threading.Thread(target=task, args=(i,)) thread_list.append(t) for thread in thread_list: thread.start() Executing above code throws this error, can anyone help me out of this I am pulling out my hair from one day Thanks in advance Answer: **Like this:** Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 Type "copyright", "credits" or "license()" for more information. >>> import socket >>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) >>> s.connect(("www.google.com",80)) >>> s.connect(("http://www.google.com",80)) Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> s.connect(("http://www.google.com",80)) File "/usr/lib/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) gaierror: [Errno -2] Name or service not known >>> **Socket not a`HTTP` connection !** **Remove`HTTP://` tag before sending a request !** EDIT: >>> import socket >>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) >>> s.connect(("digan.net",80)) >>> s.connect(("digan.net/hahaha/hihihi/etc",80)) Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> s.connect(("digan.net/hahaha/hihihi/etc",80)) File "/usr/lib/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) gaierror: [Errno -2] Name or service not known >>> **Socket can't send request to additional path. Only talk with server !**
What version of ‘unittest2’ is needed for Python 3.4 features? Question: What version of the `unittest2` library do I need for the [new features in Python 3.4's `unittest`](https://docs.python.org/3.4/whatsnew/3.4.html#unittest)? The third-party [`unittest2` library](https://pypi.python.org/pypi/unittest2/) is a very useful back-port of Python 3's `unittest` features, to work on older Python versions. By importing that third-party library, you can use the features described for Python 3's `unittest` in your Python 2 code. There are some [handy new features](https://docs.python.org/3.4/whatsnew/3.4.html#unittest) in Python 3.4's `unittest` library. (In particular I'm trying to use the “subtests” feature in a way that will just keep working when we migrate to Python 3.) To use those features in Python 2, what version of `unittest2` do I need to install? Answer: I've done a bit of digging in the repository, and it looks like: * Subtests were added in 0.6.0 ([this commit](https://hg.python.org/unittest2/rev/0daffa9ee7c1)) * SkipTest was added in 0.6.0 ([this commit](https://hg.python.org/unittest2/rev/f0946b0ba890)) * The discover changes were added in 0.6.0 ([this commit](https://hg.python.org/unittest2/rev/d94fdb8fd3a8)) * Test dropping was added in 0.6.0 ([this commit](https://hg.python.org/unittest2/rev/503d94dea23a)) I can't find any commits at all about `mock()`, so I can only presume that it isn't available or maintained. Otherwise, everything you want is in 0.6.0 or above (but I recommend you just get the latest anyway).
How to numpy searchsorted bisect a range of 2 values on 1 column and get min value in 2nd column Question: So I have a 2 column numpy array of integers, say: tarray = array([[ 368, 322], [ 433, 420], [ 451, 412], [ 480, 440], [ 517, 475], [ 541, 503], [ 578, 537], [ 607, 567], [ 637, 599], [ 666, 628], [ 696, 660], [ 726, 687], [ 756, 717], [ 785, 747], [ 815, 779], [ 845, 807], [ 874, 837], [ 905, 867], [ 934, 898], [ 969, 928], [ 994, 957], [1027, 987], [1057, 1017], [1086, 1047], [1117, 1079], [1148, 1109], [1177, 1137], [1213, 1167], [1237, 1197], [1273, 1227], [1299, 1261], [1333, 1287], [1357, 1317], [1393, 1347], [1416, 1377]]) I am using np.searchsorted to bisect lower and upper ranges of values into column 0 i.e can both times e.g 241,361 bisect into the array. ranges = [array([241, 290, 350, 420, 540, 660, 780, 900]), array([ 361, 410, 470, 540, 660, 780, 900, 1020])] e.g: np.searchsorted(tarray[:,0], ranges) This then results in: array([[ 0, 0, 0, 1, 5, 9, 13, 17], [ 0, 1, 3, 5, 9, 13, 17, 21]]) where each position in the two resulting arrays is the range of values. What I then want to do is get the position of minimum value in column 1 of the resulting slice. e.g here is what I mean simply in Python via iteration (if result of searchsorted is 2 column array 'f'): f = array([[ 0, 0, 0, 1, 5, 9, 13, 17], [ 0, 1, 3, 5, 9, 13, 17, 21]]) for i,(x,y) in enumerate(zip(*f)): if y - x: print ranges[1][i], tarray[x:y] the result is: 410 [[368 322]] 470 [[368 322] [433 420] [451 412]] 540 [[433 420] [451 412] [480 440] [517 475]] 660 [[541 503] [578 537] [607 567] [637 599]] 780 [[666 628] [696 660] [726 687] [756 717]] 900 [[785 747] [815 779] [845 807] [874 837]] 1020 [[905 867] [934 898] [969 928] [994 957]] Now to explain what I want: within the sliced ranges I want the row that has the minimum value in column 1. e.g 540 [[433 420] [451 412] [480 440] [517 475]] I want the final result to be 412 (as in [451 412]) e.g for i,(x,y) in enumerate(zip(*f)): if y - x: print ranges[1][i], tarray[:,1:2][x:y].min() 410 322 470 322 540 412 660 503 780 628 900 747 1020 867 Basically I want to vectorise this so I can get back one array and not need to iterate as it is non performant for my needs. I want the minimum value in column 1 for a bisected range of values on column 0. I hope I am being clear! Answer: This appears to achieve your intended goals, using the [numpy_indexed](https://github.com/EelcoHoogendoorn/Numpy_arraysetops_EP) package (disclaimer: I am its author): import numpy_indexed as npi # to vectorize the concatenation of the slice ranges, we construct all indices implied in the slicing counts = f[1] - f[0] idx = np.ones(counts.sum(), dtype=np.int) idx[np.cumsum(counts)[:-1]] -= counts[:-1] tidx = np.cumsum(idx) - 1 + np.repeat(f[0], counts) # combined with a unique label tagging the output of each slice range, this allows us to use grouping to find the minimum in each group label = np.repeat(np.arange(len(f.T)), counts) subtarray = tarray[tidx] ridx, sidx = npi.group_by(label).argmin(subtarray[:, 0]) print(ranges[1][ridx]) print(subtarray[sidx, 1])
Clicking buttons and filling forms with Selenium and PhantomJS Question: I have a simple task that I want to automate. I want to open a URL, click a button which takes me to the next page, fills in a search term, clicks the "search" button and prints out the url and source code of the results page. I have written the following. from selenium import webdriver import time driver = webdriver.PhantomJS() driver.set_window_size(1120, 550) #open URL driver.get("https://www.searchiqs.com/nybro/") time.sleep(5) #click Log In as Guest button driver.find_element_by_id('btnGuestLogin').click() time.sleep(5) #insert search term into Party 1 form field and then search driver.find_element_by_id('ContentPlaceholder1_txtName').send_keys("Moses") driver.find_element_by_id('ContentPlaceholder1_cmdSearch').click() time.sleep(10) #print and source code print driver.current_url print driver.page_source driver.quit() I am not sure where I am going wrong but I have followed a number of tutorials on how to click buttons and fill forms. I get this error instead. Traceback (most recent call last): File "phant.py", line 12, in <module> driver.find_element_by_id('btnGuestLogin').click() File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 269, in find_element_by_id return self.find_element(by=By.ID, value=id_) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 752, in find_element 'value': value})['value'] File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute self.error_handler.check_response(response) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":"Unable to find element with id 'btnGuestLogin'","request":{"headers":{"Accept":"application/json"," Accept-Encoding":"identity","Connection":"close","Content-Length":"94","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:35670","User-Agent":"Python-urllib/2.7" },"httpVersion":"1.1","method":"POST","post":"{\"using\": \"id\", \"sessionId\": \"d38e5fa0-5349-11e6-b0c2-758ad3d2c65e\", \"value\": \"btnGuestLogin\"}","url":"/element","urlP arsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"" ,"protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/d38e5fa0-5349-11e6-b0c2-758ad3d2c65e/element"}} Screenshot: available via screen The error seems to suggest that the element with that id does not exist yet it does. **\--- EDIT: Changed code to use WebDriverWait ---** I have changed some things around to implement WebDriverWait from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time driver = webdriver.PhantomJS() driver.set_window_size(1120, 550) #open URL driver.get("https://www.searchiqs.com/nybro/") #click Log In as Guest button element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "btnGuestLogin")) ) element.click() #wait for new page to load, fill in form and hit search element2 = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "ContentPlaceholder1_cmdSearch")) ) #insert search term into Party 1 form field and then search driver.find_element_by_id('ContentPlaceholder1_txtName').send_keys("Moses") element2.click() driver.implicitly_wait(10) #print and source code print driver.current_url print driver.page_source driver.quit() It still raises this error Traceback (most recent call last): File "phant.py", line 14, in <module> EC.presence_of_element_located((By.ID, "btnGuestLogin")) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/support/wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: Screenshot: available via screen Answer: The `WebDriverWait` approach actually works for me as is: from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.PhantomJS() driver.set_window_size(1120, 550) driver.get("https://www.searchiqs.com/nybro/") element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "btnGuestLogin")) ) element.click() No errors. PhantomJS version 2.1.1, Selenium 2.53.6, Python 2.7. * * * The issue might be related to SSL and `PhantomJS`, either work through `http`: driver.get("http://www.searchiqs.com/nybro/") Or, try [ignoring SSL errors](http://stackoverflow.com/questions/23581291/python-selenium-with- phantomjs-empty-page-source): driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])
In place replacement of text in a file in Python Question: I am using the following code to upload a file on server using FTP after editing it: import fileinput file = open('example.php','rb+') for line in fileinput.input('example.php'): if 'Original' in line : file.write( line.replace('Original', 'Replacement')) file.close() There is one thing, instead of replacing the text in its original place, the code adds the replaced text at the end and the text in original place is unchanged. Also, instead of just the replaced text, it prints out the whole line. Could anyone please tell me how to resolve these two errors? Answer: 1) **The code adds the replaced text at the end and the text in original place is unchanged.** You can't replace in the body of the file because you're opening it with the `+` signal. This way it'll append to the end of the file. file = open('example.php','rb+') But this only works if you want to **append** to the end of the document. To **bypass** this you may use [`seek()`](http://stackoverflow.com/questions/11696472/seek-function) to navigate to the specific line and replace it. Or create 2 files: an `input_file` and an `output_file`. * * * 2) **Also, instead of just the replaced text, it prints out the whole line.** It's because you're using: file.write( line.replace('Original', 'Replacement')) * * * **Free Code:** I've segregated into 2 files, an inputfile and an outputfile. First it'll open the `ifile` and save all lines in a list called `lines`. Second, it'll read all these lines, and if `'Original'` is present, it'll `replace` it. After replacement, it'll save into `ofile`. ifile = 'example.php' ofile = 'example_edited.php' with open(ifile, 'rb') as f: lines = f.readlines() with open(ofile, 'wb') as g: for line in lines: if 'Original' in line: g.write(line.replace('Original', 'Replacement')) Then if you want to, you may [`os.remove()`](http://docs.python.org/library/os.html#os.remove) the non- edited file with: * * * **More Info:** [Tutorials Point: Python Files I/O](http://www.tutorialspoint.com/python/python_files_io.htm)
Python listing last 10 modified files and reading each line of all 10 files Question: I need some help listing files in a directory and reading through each file using Python. I know how to do this using shell commands but is there a Pythonic way to do it? I would like to: 1.) List all files in a directory. 2.) Grab the last 10 modified/latest files (preferably using a wildcard) 3.) Read through each line of all 10 files Using shell commands I can: Linux_System# ls -ltr | tail -n 10 -rw-rw-rw- 1 root root 999934 Jul 26 01:06 data_log.569 -rw-rw-rw- 1 root root 999960 Jul 26 02:05 data_log.570 -rw-rw-rw- 1 root root 999968 Jul 26 03:13 data_log.571 -rw-rw-rw- 1 root root 999741 Jul 26 04:20 data_log.572 -rw-rw-rw- 1 root root 999928 Jul 26 05:31 data_log.573 -rw-rw-rw- 1 root root 999942 Jul 26 06:45 data_log.574 -rw-rw-rw- 1 root root 999916 Jul 26 07:46 data_log.575 -rw-rw-rw- 1 root root 999862 Jul 26 08:59 data_log.576 -rw-rw-rw- 1 root root 999685 Jul 26 10:15 data_log.577 -rw-rw-rw- 1 root root 999633 Jul 26 11:26 data_log.578 Linux_System# cat data_log.{569..578} Using glob I am able to list the files and open a specific file but not sure how I can list only the last 10 modified files and feed the wildcard file list to the open function. import os, fnmatch, glob files = glob.glob("data_event_log.*") files.sort(key=os.path.getmtime) print("\n".join(files)) data_event_log.569 data_event_log.570 data_event_log.571 data_event_log.572 data_event_log.573 data_event_log.574 data_event_log.575 data_event_log.576 data_event_log.577 data_event_log.578 with open(data_event_log.560, 'r') as f: output_list = [] for line in f.readlines(): if line.startswith('Time'): lineRegex = re.compile(r'\d{4}-\d{2}-\d{2}') a = (lineRegex.findall(line)) Answer: it looks alike you almost did everything already import os.path, glob files = glob.glob("data_event_log.*") files.sort(key=os.path.getmtime) latest=files[-10:] # last 10 entries print("\n".join(latest)) lineRegex = re.compile(r'\d{4}-\d{2}-\d{2}') for fn in latest: with open(fn) as f: for line in f: if line.startswith('Time'): a = lineRegex.findall(line) Edit: Especially if you have many files a better and simpler solution would be import os.path, glob, heapq files = glob.iglob("data_event_log.*") latest=heapq.nlargest(10, files, key=os.path.getmtime) # last 10 entries print("\n".join(latest)) lineRegex = re.compile(r'\d{4}-\d{2}-\d{2}') for fn in latest: with open(fn) as f: for line in f: if line.startswith('Time'): a = lineRegex.findall(line)
Python equivalent of Excel's PERCENTILE.EXC Question: I am using Pandas to compute some financial risk analytics, including Value at Risk. In short, to compute Value at Risk (VaR), you take a time series of simulated portfolio changes in value, and then compute a specific tail percentile loss. For example, 95% VaR is the 5th percentile figure in that time series. I have my time series in a Pandas dataframe, and am currently using the pd.quantile() function to compute the percentile. My question is, typical market convention for VaR is use an exclusionary percentile (ie: 95% VaR is interpreted as: there is a 95% chance your portfolio will not loose MORE than the computed number) - akin to how MS Excel PERECENTILE.EXC() works. Pandas quantile() works akin to how Excel's PERCENTILE.INC() works - it includes the specified percentile. I have scoured several python math packages as well as this forum for a python solution that uses the same methodology as PERCENTILE.EXC() in Excel with no luck. I was hoping someone here might have a suggestion? Here is sample code. import pandas as pd import numpy as np test_pd = pd.Series([15,14,18,-2,6,-78,31,21,98,-54,-2,-36,5,2,46,-72,3,-2,7,9,34]) test_np = np.array([15,14,18,-2,6,-78,31,21,98,-54,-2,-36,5,2,46,-72,3,-2,7,9,34]) print 'pandas: ' + str(test_pd.quantile(.05)) print 'numpy: '+ str(np.percentile(test_np,5)) The answer i am looking for is -77.4 Thanks, Ryan Answer: EDIT: I just saw your edit. I think you are making a mistake. The value -77.4 is actually the 99.5% percentile of your data. Try `test_pd.quantile(.005)`. I believe that you must have made a mistake in Excel when specifying your percentile. EDIT 2: I just tested it myself in Excel. For the 50-th percentile, I am getting the correct value in both Excel and Numpy/Pandas. For the 5th percentile however, I am getting -72 in Pandas/Numpy, and -74.6 in Excel. But Excel is just wrong here: it is very obvious that -74.6 is the 0.5th percentile, not the 5th... FINAL EDIT: After some testing, it seems like Excel is behaving erratically around very small values of k with the `PERCENTILE.EXC()` function. Indeed, using the function with any k < 0.05 returns an error, so 0.05 must be a threshold below which the function is not working properly. I do not know why Excel chooses to return the 0.5th percentile when asked to exclude the 5th percentile (the logical behavior would be to return the 4.9th percentile, or the 4.99th...). However, both Numpy, Pandas and Excel return the same values for other values of k. For instance, `PERCENTILE.EXC(0.5) = 6`, and `test_pd.quantile(0.5) = 6` as well. I guess the lesson is that we need to be wary of Excel's behavior ;). The way I understand your problem is: you want to know the value that corresponds to the k-th percentile of your data, this k-th percentile excluded. However, `pd.quantile()` returns the value that corresponds to your k-th percentile, this k-th percentile included. I do not think that pd.quantile() returning the k-th percentile included is an issue. Indeed, assuming you want all stocks having a Value at Risk strictly above the 5-th percentile, you would do: mask = data["VaR"] < pd.quantile(data["VaR"], 0.05) data_filt = data[mask] Because you used a "smaller than" ( < ) operator, the values which exactly correspond to your 5-th percentile will be excluded, similar to Excel's PERCENTILE.EXC() function. Do tell me if this is what you were looking for.
Using Python and elasticsearch, how can I loop through the returned JSON object? Question: My code is as follows: import json from elasticsearch import Elasticsearch es = Elasticsearch() resp = es.search(index="mynewcontacts", body={"query": {"match_all": {}}}) response = json.dumps(resp) data = json.loads(response) #print data["hits"]["hits"][0]["_source"]["email"] for row in data: print row["hits"]["hits"][0]["_source"]["email"] return "OK" which produces this truncated (for convenience) JSON: {"timed_out": false, "took": 1, "_shards": {"successful": 5, "total": 5, "failed": 0}, "hits": {"max_score": 1.0, "total": 7, "hits": [{"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "sharon.zhuo@xxxxx.com.cn", "position": "Sr.Researcher", "last": "Zhuo", "first": "Sharon", "company": "Tabridge Executive Search"}, "_id": "AVYmLMlKJVSAh7zyC0xf"}, {"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "andrew.springthorpe@xxxxx.gr.jp", "position": "Vice President", "last": "Springthorpe", "first": "Andrew", "company": "SBC Group"}, "_id": "AVYmLMlRJVSAh7zyC0xg"}, {"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "mjbxxx@xxx.com", "position": "Financial Advisor", "last": "Bell", "first": "Margaret Jacqueline", "company": "Streamline"}, "_id": "AVYmLMlXJVSAh7zyC0xh"}, {"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "kokaixxx@xxxx.com", "position": "Technical Solutions Manager MMS North Asia", "last": "Okai", "first": "Kensuke", "company": "Criteo"}, "_id": "AVYmLMlfJVSAh7zyC0xi"}, {"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "mizuxxxxto@zszs.com", "position": "Sr. Strategic Account Executive", "last": "Kato", "first": "Mizuto", "company": "Twitter"}, "_id": "AVYmLMlkJVSAh7zyC0xj"}, {"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "abc@example.com", "position": "Design Manager", "last": "Okada", "first": "Kengo", "company": "ON Semiconductor"}, "_id": "AVYmLMlpJVSAh7zyC0xk"}, {"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "007@example.com", "position": "Legal Counsel", "last": "Lei", "first": "Yangzi (Karen)", "company": "Samsung China Semiconductor"}, "_id": "AVYmLMkUJVSAh7zyC0xe"}]}} When I try: print data["hits"]["hits"][0]["_source"]["email"] it prints the first email fine but when I attempt the loop with for row in data: print row["hits"]["hits"][0]["_source"]["email"] I receive an error: TypeError: string indices must be integers Please can somebody suggest how I can iterate through the items correctly? Many thanks! Answer: What you're doing is looping through keys of the dictionary. To print each email in the response you'd do this: for row in data["hits"]["hits"]: print row["_source"]["email"] Also converting to json isn't necessary. This should accomplish what you're looking to do: from elasticsearch import Elasticsearch es = Elasticsearch() resp = es.search(index="mynewcontacts", body={"query": {"match_all": {}}}) for row in resp["hits"]["hits"]: print row["_source"]["email"] return "OK"