chophouse 25 Newbie Poster

Got this to work:

import string

lambda line: line if line[0] in string.digits else None
chophouse 25 Newbie Poster

Still working this thru. The reason for lambda is that it will be part of a pyspark filter statement, i.e.

RDD2 = RDD1.filter(lambda line: "take line only if it starts with digit")

where "take line only if it starts with digit" is the part I'm trying to figure out

chophouse 25 Newbie Poster

I'm trying to design a lambda function that returns any line in a file that begins with any digit [0-9]. The point would be to return the whole line if it does, and skip any line if it doesn't. This is for log analysis where I want all the lines that begin with an IP address.

I'm beginning to think this is too complex for a lambda expression. Any suggestions?

chophouse 25 Newbie Poster

Does anyone on the forum have any experience with pyspark (Python API for Apache Spark)

chophouse 25 Newbie Poster

It shouold throw an error in that instance

chophouse 25 Newbie Poster

What line is causing the error. Perhaps you could post the error message?

It sounds like one of your el.find statements is coming up empty (None) and that's causing the error

chophouse 25 Newbie Poster

Vegaseat,

I'd never heard of deque before. Must study. Thanks

chophouse 25 Newbie Poster

Grub,

thanks, but it only needs to shift the supplied list_in, no need for an infinite sequence

chophouse 25 Newbie Poster

Thanks pyTony,

I made two changes:
line 6 changed to work = list_in[:]

lin 17 changed to hold = list_in[-offset:]

And now all is well.

Valuable lesson there. Thanks

Grib,

Thanks for your approach which I'm still studying. I do have a question or two about it:

at line 2 offset = offset % len(list_in)
Wouldn't this always be the offset (as long as offset is < len(list_in) )? If so, then I can see that the original list_in would be returned if the offset were exactly equal to the len(list_in).

However if the supplied offset is > len(list_in), the else statement should raise an error, but as written it will apply an incorrect offset to the list_in, won't it?

chophouse 25 Newbie Poster

I've written this function to take a list and shift each member right by the number supplied as the offset.

I want to preserve the supplied list and return the shifted list.

My problem is that the function modifies the supplied input list itself as well as returning a new list with the elements shifted, but I can't figure out why. I need the supplied list to be unchanged.

My debugging shows me that the line:

work[i + offset] = list_in[i] is the culprit because each time it executes, the supplied input list changes as well.

I'm stumped as to why. Any ideas?

Here's the function:

def shift_right(list_in, offset):
    """
    Shifts list to the right by number supplied as offset
    """
    # We want to preserve the supplied list, so make a copy
    work = list_in

    # Check that offset doesn't exceed length of list 
    #  if it does return 1 as error code
    if offset >= len(list_in):
        return "1"

    else:
            if len(list_in) > 0:
                # capture fall off to the right to 
                # be re-inserted at beginning of list
                hold = list_in[offset + 1:]

                # now begin the shifting
                for i in range(len(list_in)-offset):
                    work[i + offset] = list_in[i]

                # Now take the values that fell off and put them
                # back at the beginning
                else:
                    for x in range(len(hold)):
                        work[x] = hold[x]
            return work    

# Test the function 
one = [1,2,3,4,5,6,7]
print(shift_right(one, 3))

The result I get is [5, 6, 7, 1, 2, 3, 1]

chophouse 25 Newbie Poster

I did, but it never connects.
After modifying the script to:

from ftplib import FTP_TLS
import ftplib

ftps = FTP_TLS()

ftps.connect(host="xxx.xx.xxx.xx", port=990)
ftps.login(user="myuname", passwd="mypwd")
ftps.prot_p()
ftps.set_pasv(False)
ftps.retrlines('LIST') 

ftps.quit()

It still times out, but if I comment out the line

ftps.retrlines('LIST')

It connects fine.

With that line active (uncommented) the Filezilla server log shows this as the problem:

(000050)12/18/2013 12:25:49 PM - myuname (aa.bbb.cc.dd)> LIST
(000050)12/18/2013 12:25:49 PM - myuname (aa.bbb.cc.dd)> 150 Opening data channel for directory list.
(000050)12/18/2013 12:26:00 PM - myuname (aa.bbb.cc.dd)> 425 Can't open data connection.
(000050)12/18/2013 12:28:00 PM - myuname (aa.bbb.cc.dd)> 421 Connection timed out.
(000050)12/18/2013 12:28:00 PM - myuname (aa.bbb.cc.dd)> disconnected.

So it looks like the LIST command is getting through, but it can't get the data back to my script for some reason

chophouse 25 Newbie Poster

Using Python 3.3.0 and this script:

from ftplib import FTP_TLS

ftps = FTP_TLS('xxx.xx.xxx.xx')
ftps.sendcmd('USER myuname') 
ftps.sendcmd('PASS mypwd')
ftps.prot_p()          
ftps.retrlines('LIST') 
ftps.quit()

I get a connection failure due to timeout

>>> ================================ RESTART ================================
    >>> 
    Traceback (most recent call last):
    File "C:\Python33\fixFTP.py", line 2, in <module>
      ftps = FTP_TLS('xxx.xx.xxx.xx')
    File "C:\Python33\lib\ftplib.py", line 685, in __init__
      FTP.__init__(self, host, user, passwd, acct, timeout, source_address)
    File "C:\Python33\lib\ftplib.py", line 114, in __init__
      self.connect(host)
    File "C:\Python33\lib\ftplib.py", line 148, in connect
      source_address=self.source_address)
    File "C:\Python33\lib\socket.py", line 424, in create_connection
      raise err
    File "C:\Python33\lib\socket.py", line 415, in create_connection
      sock.connect(sa)
    TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

But I can connect using a FileZilla client using these credentials. Any clue as to why it times out using a script, but not using Filezilla client?

chophouse 25 Newbie Poster

great tip. thanks

chophouse 25 Newbie Poster

I'm embarassed to admit this, but maybe it will help someone else from doing the same stupid thing. I started out writing funky.py in the 101scripts directory. Then decided I should have a separate directory for my own reusable functions so created My_Own_Stuff directory, called it out in PYTHONPATH and continued to work on it. Problem was, I left the original copy in the 101Scripts directory so it never got updated. So when 101Scripts was the working directory, my scripts were looking at the old funky.py module which didn't have the doyr function in it (but did have fileup). Lesson learned. Pay attention to your housekeeping!!

TrustyTony commented: Nice sharing mind! +12
chophouse 25 Newbie Poster

I'm having trouble calling a function I've written after importing the module containing it. The module is called funky.py. Here is the code for funky.py:

from ftplib import FTP_TLS

####    FUNCTION  ---  fileup()   #### for sending file via FTP TLS ##
## arg1 ..  destname > the filename to be created on the destination machine
## arg2 .. sourcepath > the absolute path to the source file
## usage .... fileup('foobar.txt', 'C:/dir1/dir2/orig_foobar.txt', '123.45.678.90', 'myusernm', 'mypassword')
## Note 1. Will overwrite on destination if file already exists
## 

def fileup(destfile, sourcepath, host, uname, password ):
    # open FTP connection
    ftps = FTP_TLS(host,user=uname, passwd=password)
    ftps.prot_p()                                   # Invokes protected mode
    ftps.set_pasv(False)                            # Avoids timeouts
    outbound = 'STOR ' + destfile
    with open(sourcepath, 'rb') as filein:
           ftps.storbinary(outbound, filein, 8192)
    ftps.close()


####    FUNCTION  ---  doyr()   #### generates day of year (1-366)##
## usage .... doyr()  no arguments
## returns  3 digit julian date (day of year)
##
import datetime 
def doyr():
    jd = datetime.date.today().strftime("%j")
    return(jd)


print(doyr())

It lives in the directory 'C:\Python33\My_Own_Stuff' which is included in my PYTHONPATH environmental variable (OS is Windows Server 8, R2). If I import it straight from the shell, it works fine:

>>> import sys
>>> sys.path
['C:\\Python33\\Lib\\idlelib', 'C:\\Python33\\My_Own_Stuff', 'C:\\Windows\\system32\\python33.zip', 'C:\\Python33\\DLLs', 'C:\\Python33\\lib', 'C:\\Python33', 'C:\\Python33\\lib\\site-packages']
>>> import funky
329
>>> from funky import doyr
>>> doyr()
'329'

However, if I run another script, located in 'C:\Users\user101\Documents\101_Scripts', when I try and use the 2nd function 'doyr' in the script I get an "AttributeError: 'module' object has no attribute …

chophouse 25 Newbie Poster

Thank you very much. I see now I was building a string, instead of creating the object. All working now.

chophouse 25 Newbie Poster

Need help understanding this error, and how to eliminate it. Using Python 3.3.

I'm trying to define a function that takes two arguments and then constructs the proper arguments for a call to the ftblib.storbinary method.

Here's my code:

    from ftplib import FTP_TLS

    ftps = FTP_TLS('xxxxxx', user='xxxx', passwd='xxxx')
    ftps.prot_p()
    ftps.set_pasv(False)


    ## Function for sending file ##
    def fileup (destname, sourcepath):
        outbound = 'STOR ' + destname 
        filesrc = 'open(\"'+sourcepath +'\", \'rb\')'
        print('outbound = '+ outbound)
        print('filesrc = '+ filesrc)
        print(  outbound + '        ' + filesrc)
        ftps.storbinary(outbound, filesrc, 8192)

    fileup('textgetFUNC01.txt', 'C:/inetpub/ftproot/textget1122.txt')


    ftps.close()

It creates a file (but file is 0kB) on the destination machine and throws this error:

Traceback (most recent call last):
  File "C:/pathto/funky.py", line 18, in <module>
    fileup('textgetFUNC01.txt', 'C:/inetpub/ftproot/textget1122.txt')
  File "C:/pathto/funky.py", line 16, in fileup
    ftps.storbinary(outbound, filesrc, 8192)
  File "C:\Python33\lib\ftplib.py", line 789, in storbinary
    buf = fp.read(blocksize)
AttributeError: 'str' object has no attribute 'read'

However if I comment out the function and just run this statement in its place, I get a clean execution ( file is transferred, new file with appropriate content is created on destination machine and no errors generated).

ftps.storbinary('STOR textgetFUNC01.txt', open("C:/inetpub/ftproot/textget1122.txt", 'rb'), 8192)

As far as I can tell, this is the exact same argument list I am building with my function and passing to storbinary within my function.

I've looked at line 789 in ftplib.py but don't understand what it is trying to do. So I'd like to figure out how to make my function work, and stop …

chophouse 25 Newbie Poster

I think I solved the problem by inserting " ftps.set_pasv(False) " immediately after the " ftps.prot_p() " statement. That solves the time out problem, but know I have a new problem which I'll ask as a new questio

chophouse 25 Newbie Poster

I'm writing a script to automatically up load a file from Server A to FTP server B. I'm getting stuck at the connecting part. I can connect with a python script using regular FTP, and have done so, but when trying to implement a SSL connection, I keep getting a timeout. I can't figure out why. Have searched here and stackoverflow. Any ideas welcomed!

I've verified the SSL setup on the receiving server by manually connecting using a Filezilla client

Here's my script:

from ftplib import FTP
from ftplib import FTP_TLS

ftps = FTP_TLS('xxx.xx.xxx.xx')

ftps.auth()

ftps.sendcmd('USER User') 
ftps.sendcmd('PASS Password')

ftps.prot_p()
ftps.retrlines('LIST')

ftps.close()

And the result is:

Traceback (most recent call last):
  File "Script path and name removed for posting", line 12, in <module>
    ftps.retrlines('LIST')
  File "C:\Python33\lib\ftplib.py", line 767, in retrlines
    conn = self.transfercmd(cmd)
  File "C:\Python33\lib\ftplib.py", line 381, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "C:\Python33\lib\ftplib.py", line 742, in ntransfercmd
    conn, size = FTP.ntransfercmd(self, cmd, rest)
  File "C:\Python33\lib\ftplib.py", line 343, in ntransfercmd
    source_address=self.source_address)
  File "C:\Python33\lib\socket.py", line 424, in create_connection
    raise err
  File "C:\Python33\lib\socket.py", line 415, in create_connection
    sock.connect(sa)
    TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

What am I doing wrong, or not doing?

chophouse 25 Newbie Poster

It looks like the email pkg will do the parsing, but what module would be used to actually open the email from the directory itis sitting in?

chophouse 25 Newbie Poster

Is the following possible using Python?

  1. Given a directory with 500 emails in it. These are all bounced emails.
  2. Have script open 1st email
  3. Python extracts the failed email address (from body of email, not header or addressing)
  4. Script appends found email address to separate file of bad addresses
  5. Script does 2 thru 4 to the rest of the emails, untl they've all been harvested

Result is a single file with all the bad addresses.

chophouse 25 Newbie Poster

Fixed now via two different edits.

1. Edited connections.py in pymysql directory per http://code.google.com/p/pymysql/issues/detail?id=55

old code:

def unpack_int24(n):
    return struct.unpack('B',n[0])[0] + (struct.unpack('B', n[1])[0] << 8) +\
        (struct.unpack('B',n[2])[0] << 16)

def unpack_int32(n):
    return struct.unpack('B',n[0])[0] + (struct.unpack('B', n[1])[0] << 8) +\
        (struct.unpack('B',n[2])[0] << 16) + (struct.unpack('B', n[3])[0] << 24)

def unpack_int64(n):
    return struct.unpack('B',n[0])[0] + (struct.unpack('B', n[1])[0]<<8) +\
    (struct.unpack('B',n[2])[0] << 16) + (struct.unpack('B',n[3])[0]<<24)+\
    (struct.unpack('B',n[4])[0] << 32) + (struct.unpack('B',n[5])[0]<<40)+\
    (struct.unpack('B',n[6])[0] << 48) + (struct.unpack('B',n[7])[0]<<56)

replaced with this:

def unpack_int24(n):
    try:
        return struct.unpack('B',n[0])[0] + (struct.unpack('B', n[1])[0] << 8) +\
            (struct.unpack('B',n[2])[0] << 16)
    except TypeError:
        return n[0]+(n[1]<<8)+(n[2]<<16)

def unpack_int32(n):
    try:
        return struct.unpack('B',n[0])[0] + (struct.unpack('B', n[1])[0] << 8) +\
            (struct.unpack('B',n[2])[0] << 16) + (struct.unpack('B', n[3])[0] << 24)
    except TypeError:
        return n[0]+(n[1]<<8)+(n[2]<<16)+(n[3]<<24)

def unpack_int64(n):
    try:
        return struct.unpack('B',n[0])[0] + (struct.unpack('B', n[1])[0]<<8) +\
        (struct.unpack('B',n[2])[0] << 16) + (struct.unpack('B',n[3])[0]<<24)+\
        (struct.unpack('B',n[4])[0] << 32) + (struct.unpack('B',n[5])[0]<<40)+\
        (struct.unpack('B',n[6])[0] << 48) + (struct.unpack('B',n[7])[0]<<56)
    except TypeError:
        return n[0]+(n[1]<<8)+(n[2]<<16)+(n[3]<<24) \
              +(n[4]<<32)+(n[5]<<40)+(n[6]<<48)+(n[7]<<56)
2. I added a " % " char between the end of my SQL query and the argument list. Originally it was :
cur.execute("""INSERT INTO TableX (itemID, sortID)
            VALUES (%s, %s)""",
            [plist[sid], lastQ[0] ] )

which I changed to

cur.execute("""INSERT INTO TableX (itemID, sortID)
               VALUES (%s, %s)""" % (plist[sid], lastQ[0])
           )

Now the script is running normally again.

chophouse 25 Newbie Poster

Both plist[sid] and lastQ[0] are ints

I've tried converting with both bytes and bytearray but still getting same error message

chophouse 25 Newbie Poster

How would i use that to define an interval of every 72 minutes ? Not sure I understand.

chophouse 25 Newbie Poster

My Python environment:
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 bit (AMD64)] on win32

I have a script that has been running for months. It's a cron job and suddenly it started throwing this error:
pymysql.err.Error: (<class 'TypeError'>, TypeError("'int' does not support the buffer interface",))

I don't understand why it had been working fine and just started throwing this error.

Here's the code,
followed by the full error message

import pymysql

conn = pymysql.connect(host='1.2.3.4', port = 1234, user = 'user',  passwd='passwd', db='db')
cur = conn.cursor()


lastQ = [165]
plist = [3327, 2145, 3429, 3442, 2905, 3339, 2628, 1655, 1831, 3202, 2551, 2110]


###Debug statements
print("plist")
print(len(plist))
print ("\n")

print("last[Q]")
print(lastQ[0] )
print ("\n")
lastQ[0] = lastQ[0] + 1
print(lastQ[0] )

# Code that is throwing error

for sid in range(len(plist)):
   lastQ[0] = lastQ[0] + 1
   cur.execute("""INSERT INTO TableX (itemID, sortID)
               VALUES (%s, %s)""",
               [plist[sid], lastQ[0] ] )

cur.close()
conn.close()

The Full output, including error is:

>>> ================================ RESTART ================================
>>> 
plist
12


last[Q]
165


166
Traceback (most recent call last):
  File "C:/...... script.py", line 28, in <module>
    [plist[sid], lastQ[0] ] )
  File "C:\Python33\pymysql\cursors.py", line 117, in execute
    self.errorhandler(self, exc, value)
  File "C:\Python33\pymysql\connections.py", line 187, in defaulterrorhandler
    raise Error(errorclass, errorvalue)
pymysql.err.Error: (<class 'TypeError'>, TypeError("'int' does not support the buffer interface",))
>>> 

I don't understand what the error actually is from this message

Thanks

chophouse 25 Newbie Poster

I'm not sure exactly which forum to ask this,so I'm starting here,

I have a cron job I want to execute every 72 minutes. I've researched how to schedule the job for an interval of 60 min or less, but is there a way to specify a minutes interval when the required interval is > 60 ?

chophouse 25 Newbie Poster

It was working for me on 3.2. I thought I'd read somewhere ir had been revised for 3.3?

chophouse 25 Newbie Poster

I just moved to a new server and installed Python 3.3. On my old box i was using Python 3.2. Now my scripts that import pymysql won't work, saying the module doesn't exist. Wher do I download this from? I'm on Win Server 2008 R2 (on both boxes)

Thanks

chophouse 25 Newbie Poster

Use a cron job. If you are on a windows platform (I am) I'd recommend Windows Cron Service by Intelladmin http://www.intelliadmin.com/?p=5079

chophouse 25 Newbie Poster

Guessing here, but maybe you need to give the full absolute path:
'C:\Users\spreston\Desktop\HeadFirst\Chapter 3'

assuming that is what the full absolute path is, of course

chophouse 25 Newbie Poster

Thanks pyTony Changing the path to just the C:\Python instead of adding the .exe file also, seems to have solved the problem.

and yes, when I said 'Run Module' I meant Run > Run Module from the top menu (same as F5)

Thanks again

chophouse 25 Newbie Poster

Here's the details from the pythonw.exe error window:

Problem signature:
  Problem Event Name:   APPCRASH
  Application Name: pythonw.exe
  Application Version:  0.0.0.0
  Application Timestamp:    4f8512b6
  Fault Module Name:    StackHash_c0cd
  Fault Module Version: 6.1.7601.17725
  Fault Module Timestamp:   4ec4aa8e
  Exception Code:   c0000374
  Exception Offset: 00000000000c40f2
  OS Version:   6.1.7601.2.1.0.272.7
  Locale ID:    1033
  Additional Information 1: c0cd
  Additional Information 2: c0cd77b1604132f060c407509433b7a3
  Additional Information 3: de6e
  Additional Information 4: de6e0c4419adecb96dad5126c42a8745

Not sure why its using pythonw.exe is throwing the error message since I put python.exe in the path

chophouse 25 Newbie Poster

It's just that I've gotten so used to writing within the IDLE environment I'd like to be able to run from where I edit.

I've done some more experimenting. It appears things are crashing when the 'Run Module' tries to save the file. It presents me with what looks like the standard Windows explorer 'Save as' window to select a destination and name the file. If I save to Documents or a subdirectory, the crash happens, if I save to the Python32 directory all works as expected. IN my local PC version, I can save anywhere I want. Don't understand why Python in Win Srvr 2008 limits me to save only to the Pyhton32 directory. I've added C:\Python32\python.exe to my PATH.

chophouse 25 Newbie Poster

I'm getting an error/crash when trying to use Python on my virtual server running Win 2008 Server.

Python installed without incident, and IDLE opens and the interactive widnow works fine. But when I try to open a new window and run it it crshes when trying to save.

Example:

I Select 'New Window' from the Python Idle File menu. Type in my simple test code: print(foobar test)

Then when I select Run from the top menu and try to save it in the pop up save box It it crashes, showing me an error message that 'python.exe has stopped working'. Then all IDLE windows go away and nothing is saved.

chophouse 25 Newbie Poster

I'm new too (couple months), but you have to write code, not forever think about what might work.

Try something, get it to work, then try the next thing.

For instance write the code to ask the user to input a list of 20 integers. Make that work, then try the next step of analyzing the numbers. Post here (post the code) when you get stuck. Or if it's easier for you, write the code to do the analysis and hard code the 20 integers. Get that to work, then write the input part. Once both are done, combine them, substituting the input 20 integers for the hard coded 20 part.

But ya gotta write something first !

chophouse 25 Newbie Poster

Did a bit more research and discovered the magic of strftime which I think solves my problem.

I replaced lines 12 thru 21 in my original example with:

# gets date of last Tuesday
endwk = nowdate - datetime.timedelta(days = wkday)  # need this later as a datetime object
d1 = endwk.strftime("%Y%m%d")


# gets date of the Tuesday a week before last Tuesday
startwk = endwk - datetime.timedelta(days = 7) 
d2 = startwk.strftime("%Y%m%d")
Gribouillis commented: excellent ! +13
chophouse 25 Newbie Poster

I have a question about a better way to do this as what I've written seems clunky. I'm trying to generate 2 variables here that will be used as the date in a BETWEEN clause in MySQL. In order to get the proper construction (YYYYMMDD) I am converting to a string and slicing. But because I use the first date (endwk) as a reference to calculate the second (startwk) I need to also store the result of endwk as its original datetime (endwkdate) type in order to use it in the calculation of startwk because I get an error if I just use endwk. This seems like a rookie workaround to me, but it does work.

However I'm new to python and trying to develop good habits, so I'd be interested to know a more efficient way to do this. Any ideas, or am I just being too picky? Here's the code:

## Goal is to produce 2 dates:  Date of last Tuesday (or today if this is Tuesday)
## Date of Tuesday a week before last Tuesday

import datetime

#  day of week as an integer to be used as an offset.  If 0 then its Monday
#  Looking for Tuesday so subtract 1 from day of week return for Tuesday offset
wkday = datetime.date.weekday(datetime.datetime.now()) - 1

nowdate= datetime.datetime.date(datetime.datetime.now())

# gets date of last Tuesday
endwkdate = nowdate - datetime.timedelta(days = wkday)  # need this later as a datetime object

endwk= str(nowdate - datetime.timedelta(days = wkday))  # as …
chophouse 25 Newbie Poster

Got it to work using the os methods. Thanks

chophouse 25 Newbie Poster

pyTony,

using different names for logfile in the two different opens

Not sure I understand here. Are you saying why the full path in the os.path.exists argument and only the file name in the opens? Because it looks like the same filename to me in the 2 opens. In each case "logfile.txt".

It does work manually as written, but then I'm already in that directory. Hmmm, so if I put full path in the opens you think it might work?? I'll try that.

Gribouillis,

I haven't any experience with exceptions yet, but will also give your example a try. I like that os.path.join function. Much easier to read and understand.

Thanks

chophouse 25 Newbie Poster

This is part of another script, too long for here, but isolating this part, my dilemma is that this works when manually invoked from IDLE, but not when run as a cron job.

from datetime import datetime
import os

# check to see if a log file exists, make one if it doesn't
if os.path.exists("C:\\Dir1\\Sub One\\Sub3\\Sub4\\Sub Five\\logfile.txt"):
        log = open('logfile.txt', 'r')
else:
    log = open('logfile.txt', 'w+')

The rest of the script runs as a cron and does its tasks, but this log never gets done, whether creating a new file because none exists or opening an existing logfile.

Any ideas why this isn't triggered when run as a cron?

Using IntellAdmin Windows Cron 2 Config on windows 7 machine.

chophouse 25 Newbie Poster

Has anybody written or know of a log parser I can use to parse Shoutcast (v2) logs?

chophouse 25 Newbie Poster

I ended up doing it in Python by following the above procedure. Works like a charm.

chophouse 25 Newbie Poster

Perfect !! Thanks. Seems so easy, once it's pointed out!

chophouse 25 Newbie Poster

I'm writing a script to populate a list with the last 5 albums frm a list of what's about to play. My query works in terms of retrieving the values, but the list populates with a set of parens and an extra comma added to each returned value. Any ideas on how to get rid of them?

Here's the code:

# Populate 'toosoon' with last 5 albums in current queue

# Further up I import pymysql and then do the connection

toosoon = []

#  Following is my query as written, which is how I run it
cur.execute("select album from songlist where ID in (SELECT SongID from queuelist where sortID in ((select max(SortID)from queuelist), (select max(SortID) -1 from queuelist), (select max(SortID) -2 from queuelist), (select max(SortID) -3 from queuelist), (select max(SortID) -4 from queuelist)))")

# here's the same query but with word wrap to make ir readable
# select album from songlist 
#   where ID in 
#   (SELECT SongID from queuelist 
#       where sortID in (
#           (select max(SortID)from queuelist), 
#           (select max(SortID) -1 from queuelist), 
#           (select max(SortID) -2 from queuelist), 
#           (select max(SortID) -3 from queuelist), 
#           (select max(SortID) -4 from queuelist)
#           )
#    )

for album in cur:
   toosoon.append(album) # adds each album name of returned result to 'toosoon'

cur.close()
conn.close()

print(toosoon)

This returns the list 'toosoon' as:
[('Little Big Men',), ('Hard Believer',), ('Up2zero',), ('Blues for the modern daze',), ('Breathe Deep',)]

but in order to use it later, the 'toosoon' list needs to …

chophouse 25 Newbie Poster

I would want all rows to meet it. I'm thinking I may have to script this in Python and use the original random list as a feedstock, then evalaute each one. Where it passes, send it to finallist, where it doesn't save it off to failed. Then at the end do again with everything in failed. I'll post here if I ever get it right.

chophouse 25 Newbie Poster

Is there a way to use Order by RAND() for a query so that it returns a list of results and guarantees that adjacent values will be at least 'x' from each other, on either side? The results are all numbers, no strings.

So if it returns 5 results r1 thru r5
abs(r2 - r1) is > 15
abs(r3- r2) is > 15
abs(r4 - r3) is > 15
abs(r5 - r4) is > 15

Eventually I want to get to the point where I can guarantee that no sooner than r6 will a value be < 15 from r1 (remembering that the results are deliberately not sorted iin order, but rather, are random - except for that spacing minimum of 15). Then r7 would be the soonest that a value would be < 15 from r2, etc.

chophouse 25 Newbie Poster

Great. Thanks much

chophouse 25 Newbie Poster

Thanks. That was quick!! But I'd like it to store that value in the list that I am creating out of the result

for instance:

cur.execute("Select track_id, track_duration from tracks where track_id = 342")
for r in cur:
    print(r)

and I get this: (342, datetime.timedelta(0, 170))

but what I want is (342, 0:02:50) stored as r

chophouse 25 Newbie Poster

My first Daniweb question!! Am at the front end of learning Python (3.2.3)

I'm doing a SQL query into a MySQL database for two fields, track_ID and track_duration. I'm getting a return of both, but instead of the duration value being expressed as 00:02:50, as it is stored in the database, my Python call is returning it as datetime.timedelta(0,170). I realize that is 170 seconds and is correct, but would like to replicate the formatting in the db.

How do I get it to return it in the form it was stored, instead of the datetime.timedelta function?

MySQL call is: cur.execute("Select track_id, track_duration from tracks where track_id = 342") track_duration is stored in my db as a time data type.

chophouse 25 Newbie Poster

Over the last couple weeks my Outlook program has been automatically sending what I think are mass emails to addresses with a .ru suffix. I only know this because I periodically (about 2x a week) get failure notices where an email address was undeliverable. I usually get about a dozen failed deliveries when this happens.

There is nothing in my Sent folder indicating anything is sent so I suspect I’ve been hijacked or had a worm install itself.

Anybody have any recent experience with this scenario?

I’m running XP Home SP2, Outlook 2000 SP-3 (9.0.0.8954), and if it’s relevant IE 7.0.5730.11

I can post the headers from some of the failed deliveries if that would help.

Can anyone tell me what I should do to get rid of this problem?

Thanks