Crush | 25 Oct 21:09 2014
Picon

Re: Code critique

Thank you Peter for your example. I have the code working now and will post soon for eveyones benefit. 

Thank you all who took the time to help. 

Bo
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Mark Meanwell | 25 Oct 18:08 2014
Picon

Help with running an API

Hi Folks - new to python and trying to run an API. Running version 2.7.3. on Windows 7 machine.

 Here is the scenario for the given API (FRED API in this case):

easy_install Fred from C:\ - this installs to C:\site packages

then I fire up the python shell and run file created for fred api:

from fredapi import Fred
fred = Fred(api_key='my api key')
data = fred.get_series('SP500')

keep getting below error message: 
ImportError: cannot import name Fred

thanks for any suggestions!
 
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Clayton Kirkwood | 25 Oct 05:17 2014
Picon

solution for for loop?

description_string=code_string=''

description = code = ‘a’

for (description_position, code_position) in (description, code):

    print(description_position,code_position)

 

I have tried variations on this for statement, and it doesn’t work:<))) Both description and code have the same size array. I was hoping that some derivative of this for would bring in a new description_position value, and code_position value.

Amongst various trials I have tried dp in d && cp in c; dp, cp in d,c. etc.

 

This is the error report:

Traceback (most recent call last):

  File "C:/Users/Dad/python/stock tracker/raw yahoo scraper codes.py", line 80, in <module>

    for (description_position, code_position) in (description, code):

ValueError: too many values to unpack (expected 2)

 

Is there something like what I want?

 

Thanks,

 

CLayton

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Friedhelm Peters | 24 Oct 16:31 2014
Picon
Picon

Problem by installing Python 3.4.2 64 Bit

Dear Mr. and Mrs.,

 

Sorry, I’m from Germany and my English isn’t so good. By installing python 3.4.2 (64bit) I get the following error message:

 

“There is a problem with this Windows Installer package. A program required for this install the complete could not be run. Contact your support personnel or package vendor.”

 

My system: Windows 7 Ultimate x64 SP1, Intel i7 3770, 16 GB RAM

 

The Version 3.4.1150.1013 is installed

 

Greetings

 

Friedhelm Peters

 

Bockholter Str. 29

 

45659 Recklinghausen

 

Tel.:        +49 (171) 823 899 1

Fax:       +49 (2361) 905 326

Mail:       friedhelm.peters.1009 <at> epost.de

                Friedhelm-peters <at> arcor.de

 

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Bo Morris | 24 Oct 14:03 2014
Picon

Code critique

Hello all, 

May I please get a little instructional criticism. The code below works. It logs into 9 different Linux computers, runs a couple commands, and then transfers a file back to the server. I want to become a better Python coder; therefore, I was hoping for some ways to make the below code better, more efficient, or if I am doing something incorrectly, a correct way of doing it.  Thanks

#!/usr/bin/python

import paramiko

l = ['ip-1', 'ip-2', 'ip-3', 'ip-4', 'ip-5', 'ip-6', 'ip-7', 'ip-8', 'ip-9']

def connect(ip):
    user = 'user'
    passwd = ''
    command = 'echo $HOSTNAME'
    s = paramiko.SSHClient()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(ip,22,user,passwd,timeout=4)
    stdin, stdout, stderr = s.exec_command('echo $HOSTNAME')
    out = stdout.read()
    if '3102EHD-Lanka-1108' in out:
        s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
        sftp = s.open_sftp()
        sftp.get('/Downloads/Hourly/3102EHD-01108/3102EHD-01108.png', '/Downloads/Hourly/3102EHD-01108.png')
        sftp.close()
        print 'file recieved'    
    elif '3102EHD-01109' in out:
        s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
        sftp = s.open_sftp()
        sftp.get('/Downloads/Hourly/3102DHD-01109/3102DHD-01109.png', '/Downloads/Hourly/3102DHD-01109.png')
        sftp.close()
        print 'file recieved'
    elif '3102EHD-MUTV-1082' in out:
        s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
        sftp = s.open_sftp()
        sftp.get('/Downloads/Hourly/3102EHD-01082/3102EHD-01082.png', '/Downloads/Hourly/3102EHD-01082.png')
        sftp.close()
        print 'file recieved'
    elif '3102DHD-MUTV-1033' in out:
        s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
        sftp = s.open_sftp()
        sftp.get('/Downloads/Hourly/3102DHD-01033/3102DHD-01033.png', '/Downloads/Hourly/3102DHD-01033.png')
        sftp.close()
        print 'file recieved'
    elif 'Encoder' in out:
        s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
        sftp = s.open_sftp()
        sftp.get('/Downloads/Hourly/3102EHD-01302/3102EHD-01302.png', '/Downloads/Hourly/3102EHD-01302.png')
        sftp.close()
        print 'file recieved'
    elif '3102DHD-01149' in out:
        s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
        sftp = s.open_sftp()
        sftp.get('/Downloads/Hourly/3102DHD-01149/3102DHD-01149.png', '/Downloads/Hourly/3102DHD-01149.png')
        sftp.close()
        print 'file recieved'
    elif '3102EHD-01125' in out:
        s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
        sftp = s.open_sftp()
        sftp.get('/Downloads/Hourly/3102EHD-01125/3102EHD-01125.png', '/Downloads/Hourly/3102EHD-01125.png')
        sftp.close()
        print 'file recieved'
    elif '3102DHD-01144' in out:
        s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
        sftp = s.open_sftp()
        sftp.get('/Downloads/Hourly/3102DHD-01144/3102DHD-01144.png', '/Downloads/Hourly/3102DHD-01144.png')
        sftp.close()
        print 'file recieved'
    elif '3102EHD-01105' in out:
        s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
        sftp = s.open_sftp()
        sftp.get('/Downloads/Hourly/3102EHD-01105/3102EHD-01105.png', '/Downloads/Hourly/3102EHD-01105.png')
        sftp.close()
        print 'file recieved'

con = map(connect, l)










_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Adam Jensen | 24 Oct 03:45 2014
Picon

Standard Library Performance (3.4.1)

I'm tinkering this evening and I've noticed that math.factorial() is
much faster than my plain python implementations.

--------------------------------------------
import math

def factorial(n):
    temp = 1
    for k in range(0,n):
        temp = temp * (n - k)
    return(temp)

def fac(n):
    return 1 if (n == 0) else n * fac(n-1)
--------------------------------------------

From IPython:

In [21]: %timeit factorial(9)
100000 loops, best of 3: 5.31 µs per loop

In [22]: %timeit fac(9)
100000 loops, best of 3: 6.86 µs per loop

In [23]: %timeit math.factorial(9)
1000000 loops, best of 3: 416 ns per loop
--------------------------------------------

Is this kind of performance difference typical of the standard library
functions (compared to plain python user implementations)?
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

boB Stepp | 24 Oct 05:35 2014
Picon

Is there a convenient table of Python 3.4 exceptions?

I am reading a brief intro to exception handling in Mark Summerfield's "Programming in Python 3, 2nd ed." He gives the basic syntax as:

try:
    try_suite
except exception1 as variable1:
    exception_suite1
...
except exceptionN as variableN:
    exception_suiteN

My current understanding is that exception1, ... , exceptionN should match one of Python's exception classes or subclasses in order to perform a match and execute the corresponding exception suite.

I found this in the docs:

Exceptions are identified by class instances. The except clause is selected depending on the class of the instance: it must reference the class of the instance or a base class thereof. The instance can be received by the handler and can carry additional information about the exceptional condition.

Note Exception messages are not part of the Python API. Their contents may change from one version of Python to the next without warning and should not be relied on by code which will run under multiple versions of the interpreter.

​I have so far been unable to find a list of these class/subclass names. Of course I can force an error to occur in the interpreter and see what comes up for each type of error I wish to catch. Is there such a table or list?​

--
boB
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Clayton Kirkwood | 22 Oct 06:54 2014
Picon

yet another misunderstanding on my part

__author__ = 'SYSTEM'

import string

#Pricing                Dividends

raw_table = ('''

a: Ask    y: Dividend Yield

b: Bid     d: Dividend per Share

b2: Ask (Realtime)           r1: Dividend Pay Date

b3: Bid (Realtime)            q: Ex-Dividend Date

p: Previous Close

o: Open

 

import re, string

col_position, code, description = 0, [], []

key_name = raw_table.replace('\t','\n')

for each_line in  key_name.splitlines():

    if ':' in each_line:

       code[col_position], description.append()  = each_line.split(‘:’)           #neither works; first one is out of range error, 2nd, can’t assign to

                                                                                                #function. I’ve used square brackets around various sections, and it doesn’t like it

        c, d = each_line.split(':')                                        #this works

        code[col_position] =  each_line.split(':')        #why doesn’t this. It looks like what is in the tutorial 5.1. I get out of range error

        code.append(c)                                                        #part of the first ‘this works’ line

        code[col_position] = 5                                           #this works, yet I am using the same col_position element

        description.append(d)

        print( col_position, code[col_position], description[col_position])

        col_position += 1

 

I’ve seen an example of description.strip() work from a for in loop assignment with out the can’t assign to function.

I’d like to see something like:

code[col_position].strip(), description[col_position].stripe() = each_line.split(‘:’)

but for some reason, the [col_position] doesn’t work, and I can’t strip().

 

What am I not seeing? I *do* spend hours trying different options, and study various documentation which confuse me and don’t seem consistent.

 

Thanks,

 

Clayton

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Clayton Kirkwood | 22 Oct 01:54 2014
Picon

A couple of somewhat esoteric questions

As I’ve contemplated the usage of dictionaries, I face the question of efficiency. Going back before most of you were probably born;<)) if I remember correctly dictionaries(assoc. arrays), having hashes, are efficient for storing sparse arrays with the added benefit of hiding the traversal of the dictionary looking for the proper key, and being much less efficient if looking for the value and trying to retrieve its key. But it also depends on the hash key and algorithm and how many spaces are “available” for filling. If a hash is created for a small array, which is known ahead of time with some possible hints, the array is efficient but has the distinct difficulty of use when more and more pairs are created, because either the new pair matches an already used hash point and a trail must be descended off of that particular hash point or a mechanism must be used to determine how the new pair should be stored elsewhere in the array like at the end and matching becomes time consuming. And/or, a new hash key and perhaps a different algorithm must be used for creating the larger array. Also, since the array allows for multiple keys pointing to possibly identical values, the array is not necessarily 1 to 1 (could be 1 to multiple although we use replacement of the value normally) and definitely cannot be onto: there is no rule that says the mapping from value back to key is singular and unique.

 

I’ve not had much use of dictionaries in the past so maybe I am missing something. As I contemplated dictionary use, it seems changing the order of entries is best left to double single arrays – which can be modified together when changing order. Dicts don’t seem to support moving pairs around and in fact would be broken if changes were attempted. Kind of like using a real dictionary, you can have a word (key) have multiple meanings (values), and you can have multiple words (keys) having the same meaning (value). Again, with the difference from a real dictionary, our dicts can’t have keys pointing to different values. But there are definitely uses for dicts.

 

Seem right???

 

Clayton

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Wilson, Pete | 22 Oct 02:04 2014

Passing Data to .DLL

I am having problems passing a pointer and uint8 to a .DLL. I have been successful registering call –backs with this .DLL. So it is all 32-bit and ctypes are working. Everything is working up to the line #set-up ProcessingIncomingSerialData. I tried the direct approach and then morphing the call-back style but yummy_thing is not so yummy… The python code is below..

 

The definition from the .DLL header is

 

# Python code starts here…..

 

''' read_bat.py

'''

 

from serial import *

from string import *

from time import *

 

system_state = "G2G"

 

''' --- REMOVED Set-up the COMM port ----

print("Enter COM port number")

user_comm_port = raw_input()

 

try:

    dut_serial_port = Serial(port="COM"+user_comm_port, baudrate=115200, timeout=1)

except:

    system_state = "FUBAR"

    print "Serial Port Problem"

 

try:

    dut_serial_port.isOpen()

    print("COM" + user_comm_port + " is open")

except:

    print "Serial Port Problem"

    system_state = "FUBAR"

 

-------------------------------------------'''

 

#Set-up and register cb_send_serial_data

 

from ctypes import *

 

pt_dll = CDLL("c:/py_stuff/ProductionTest.dll")

 

SendSerialData_t = CFUNCTYPE(None, POINTER(c_uint8), c_uint8)

 

reg_send_serial_data = pt_dll.RegSendSerialData

reg_send_serial_data.argtypes = [SendSerialData_t]

reg_send_serial_data.restype = None

 

 

global new_serial_tx_data

global new_serial_size

 

def send_serial_data(tx_data, size):

    # testing

    print "tx_data = ", tx_data

    print "size = ", size

    print "tx_data[:size] = ", tx_data[:size]

 

    global new_serial_tx_data

    new_serial_tx_data = tx_data[:size]

 

    global new_serial_size

    new_serial_size = size

 

cb_send_serial_data = SendSerialData_t(send_serial_data)

global cb_send_serial_data

 

reg_send_serial_data(cb_send_serial_data)

 

print "reg_send_serial_data done"

 

#Set-up and register cb_bat_vol_read

#this triggers send_serial_data when it is registered.

 

 

BatVolReadRequest_t = CFUNCTYPE(None, c_uint16, c_uint8)

 

prod_bat_vol_read_request = pt_dll.ProdBatVolReadRequest

prod_bat_vol_read_request.argtypes = [BatVolReadRequest_t]

prod_bat_vol_read_request.restype = None

 

def bat_vol_read(bat_vol, status):

    print "bat_vol_read()"

    # testing

    print bat_vol, status

 

cb_bat_vol_read = BatVolReadRequest_t(bat_vol_read)

 

prod_bat_vol_read_request(cb_bat_vol_read)

 

print "prod_bat_vol_read_request done"

 

 

''' ------------REMOVED serial access for example -----

#push new_serial_tx_data  out the serial port to the DUT.

 

print new_serial_tx_data

 

dut_serial_port.write(new_serial_tx_data)

dut_serial_port.write("\r \n")

sleep(1)

 

 

#and check to see the DUT's reply...

 

 

global new_serial_rx_data

#new_serial_rx_data =""

global rx_buf

rx_buf = []

 

try:

    string_buf = dut_serial_port.readline()   

except:

    system_state = "FUBAR"

    print "serial read problem"

 

rx_buf = list(string_buf)

print "string_buf = ", string_buf

print "rx_buf = ", rx_buf

 

-----------------------------------------------'''

 

#set-up ProcessingIncomingSerialData

 

print "breakpoint"

 

class rx_data_t:

    def _init_(self):

        #self.ret = None

        self.data = []

        self.size = ''

 

fake_rx_data = rx_data_t()

 

fake_rx_data.data = ['\x01', '\x05', '\x00', '\x1c', '\x00', '\x99', '\x0c', '\x04']

fake_rx_data.size = 8

 

print "fake_rx_data.data = ", fake_rx_data.data

print "fake_rx_data.size = ", fake_rx_data.size

 

ProcessIncomingSerialData_t = CFUNCTYPE(None, POINTER(c_uint8), c_uint16)

 

process_incoming_serial_data = pt_dll.ProcessIncomingSerialData

process_incoming_serial_data.argtypes = [ProcessIncomingSerialData_t]

#process_incoming_serial_data.argtypes = [POINTER(c_uint8), c_uint16]

process_incoming_serial_data.restype = None

 

yummy_thing = ProcessIncomingSerialData_t(fake_rx_data)passing pointers to

 

process_incoming_serial_data(yummy_thing)

#process_incoming_serial_data(fake_rx_data)

 

print "Done."

print "system_state = ", system_state

#print "Closing COM port", user_comm_port

#dut_serial_port.close()

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Nathan Spencer | 21 Oct 23:14 2014
Picon

Full screen mode

Hi there,

I'm running python on Scientific Linux.  The problem is that I'm permanently stuck in full screen mode.  Exiting out of that mode (via F11) doesn't do anything.  Do you have any suggestions?

Thanks,
Nathan

--
Nathaniel J. Spencer, PhD
Post-Doctoral Research Associate
Psychoacoustics Lab
School of Health and Rehabilitative Sciences
University of Pittsburgh
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Gmane