Issues posting data with locust file - python-3.x

def post_driver(l):
def id_generator(size = 6, chars = string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
data = {"startLocation":{"address":{"fullAddress":"\n, ","fullAddressInLine":", , ","line1":"10407 Water Hyacinth Dr","city":"Orlando","state":"FL","zip":"32825"},"latitude":28.52144,"longitude":-81.23301},"endLocation":{"address":{"fullAddress":"\n, ","fullAddressInLine":", , ","line1":"1434 N Alafaya Trail","city":"Orlando","state":"FL","zip":"32819"},"latitude":28.52144,"longitude":-81.23301},"user":{"firstName":"Test" + id_generator(),"lastName":"doe","Username":"","Password":"","userId":"","role":"","email":"zbaker#productivityapex.com","username":"Test" + id_generator() + "","newPassword":"test"},"vehicle":{"vehicleNumber":"" + id_generator(),"licensePlate":"3216549877","maxCapacity":200000,"maxCapacityUnits":"units","costPerMile":3500,"fixedCost":35000,"costPerHour":35,"odometer":0,"tags":[{"text":"vehicle"}],"constraintTags":[],"id":"59fcca46520a6e2bb4a397ed"},"earliestStartTimeDate":"Mon Nov 13 2017 07:00:00 GMT-0500 (Eastern Standard Time)","restBreakDurationDate":"Mon Nov 13 2017 01:00:00 GMT-0500 (Eastern Standard Time)","maximumWorkingHours":9,"maximumDrivingHours":8,"fixedCost":0,"costPerMile":0,"costPerHour":0,"restBreakWindows":[{"startTime":"2017-11-03T19:30:35.275Z","endTime":"2017-11-04T05:00:35.275Z"}],"color":"#1c7c11","tags":[{"text":"test"}],"driverNumber":"" + id_generator(),"phoneNumber":"7894651231","earliestStartTime":"07:00:00","restBreakDuration":"01:00:00","customFields":{}}
# json_str = json.dumps(data)
r = l.client.post('/api/v1/drivers', data=str(data))
# content = r.json()
# return content["id"]
I am currently having issues with posting the data variable and having it succeed in my locust file. I've done this with other locust files with similar formats, but it's failing every time on this particular request... can someone please help me?

Related

Convert paypal date to string in python

paypal is giving me this format '03:00:00 Mar 14, 2023 PDT'. I've tried different solutions but i can't get with the right one. How can i separate each item into a string using python?
Assuming you want a datetime object
from datetime import datetime
time_string = "03:00:00 Mar 14, 2023 PDT"
d = datetime.strptime(time_string[:-4], "%H:%M:%S %b %d, %Y")
or
import parsedatetime as pdt # $ pip3 install parsedatetime
cal = pdt.Calendar()
time_string = "03:00:00 Mar 14, 2023 PDT"
d = cal.parseDT(time_string)[0]
Once you have your datetime, output the part(s) you want using strftime

Finding regex pattern of date on separate lines and between text

I got the regex pattern of 15 July - 3 September 2022 as
[\d]{1,2} [ADFJMNOS]\w* [\-] [\d]{1,2} [ADFJMNOS]\w* [\d]{4}
My doubts are
What will be the regex pattern if the date is not on a single line
example
15 July - 3 September
2022
22
July
Desired Output
15 July - 3 September 2022
22 July
Also if it is seen as a part of another word
example
delayed15 July – 3 Septemer 2022
Here it is attached with the word "delayed". The word can be anything.
Desired Output
15 July – 3 Septemer 2022
Code i am trying
from selenium import webdriver
from bs4 import BeautifulSoup
import re
url_list = ['https://alabamasymphony.org/event/bachmozart']
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.3"
}
driver = webdriver.Chrome('/home/ubuntu/selenium_drivers/chromedriver')
format_list = ["[\d]{1,2} [ADFJMNOS]\w* [\-] [\d]{1,2} [ADFJMNOS]\w* [\d]{4}"]
for URL in url_list:
date = []
driver.get(URL)
driver.implicitly_wait(2)
data = driver.page_source
cleantext = BeautifulSoup(data, "lxml").text
cleanr = re.compile('<.*?>')
x = re.sub(cleanr, ' ', cleantext)
print(URL)
for pattern in format_list:
all_dates = re.findall(pattern, x)
if all_dates == []:
continue
else:
date.append(all_dates)
for s in date:
print(s)
You can try this regex (Demo):
(\d{1,2})\s*([A-Za-z]+)[ \t]*(?:(-\s*\d{1,2}\s*[A-Za-z]+)\s+(\d{4}))?
The idea is to break down the date and text in 4 groups and print those as needed by you.
source code (run here) :
import re
regex = r"(\d{1,2})\s*([A-Za-z]+)[ \t]*(?:(-\s*\d{1,2}\s*[A-Za-z]+)\s+(\d{4}))?"
test_str = ("15 July - 3 September 2022 as\n\n\n"
"15 July - 3 September \n"
"2022\n"
"22\n"
"July\n\n\n\n"
" Also if it is seen as a part of another word\n\n"
"example\n\n"
"delayed15 July - 3 Septemer 2022\n\n"
"Here it is attached with the word \"delayed\". The word can be anything.\n\n\n")
matches = re.finditer(regex, test_str, re.MULTILINE)
for match in matches:
if match.group(3) is not None :
print(match.group(1)+" "+match.group(2)+match.group(3)+" "+match.group(4))
else:
print(match.group(1)+" "+match.group(2))

Python Download Latest CSV from FTP server [duplicate]

I am using ftplib to connect to an ftp site. I want to get the most recently uploaded file and download it. I am able to connect to the ftp server and list the files, I also have put them in a list and got the datefield converted. Is there any function/module which can get the recent date and output the whole line from the list?
#!/usr/bin/env python
import ftplib
import os
import socket
import sys
HOST = 'test'
def main():
try:
f = ftplib.FTP(HOST)
except (socket.error, socket.gaierror), e:
print 'cannot reach to %s' % HOST
return
print "Connect to ftp server"
try:
f.login('anonymous','al#ge.com')
except ftplib.error_perm:
print 'cannot login anonymously'
f.quit()
return
print "logged on to the ftp server"
data = []
f.dir(data.append)
for line in data:
datestr = ' '.join(line.split()[0:2])
orig-date = time.strptime(datestr, '%d-%m-%y %H:%M%p')
f.quit()
return
if __name__ == '__main__':
main()
RESOLVED:
data = []
f.dir(data.append)
datelist = []
filelist = []
for line in data:
col = line.split()
datestr = ' '.join(line.split()[0:2])
date = time.strptime(datestr, '%m-%d-%y %H:%M%p')
datelist.append(date)
filelist.append(col[3])
combo = zip(datelist,filelist)
who = dict(combo)
for key in sorted(who.iterkeys(), reverse=True):
print "%s: %s" % (key,who[key])
filename = who[key]
print "file to download is %s" % filename
try:
f.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
except ftplib.err_perm:
print "Error: cannot read file %s" % filename
os.unlink(filename)
else:
print "***Downloaded*** %s " % filename
return
f.quit()
return
One problem, is it possible to retrieve the first element from the dictionary? what I did here is that the for loop runs only once and exits thereby giving me the first sorted value which is fine, but I don't think it is a good practice to do it in this way..
For those looking for a full solution for finding the latest file in a folder:
MLSD
If your FTP server supports MLSD command, a solution is easy:
entries = list(ftp.mlsd())
entries.sort(key = lambda entry: entry[1]['modify'], reverse = True)
latest_name = entries[0][0]
print(latest_name)
LIST
If you need to rely on an obsolete LIST command, you have to parse a proprietary listing it returns.
Common *nix listing is like:
-rw-r--r-- 1 user group 4467 Mar 27 2018 file1.zip
-rw-r--r-- 1 user group 124529 Jun 18 15:31 file2.zip
With a listing like this, this code will do:
from dateutil import parser
# ...
lines = []
ftp.dir("", lines.append)
latest_time = None
latest_name = None
for line in lines:
tokens = line.split(maxsplit = 9)
time_str = tokens[5] + " " + tokens[6] + " " + tokens[7]
time = parser.parse(time_str)
if (latest_time is None) or (time > latest_time):
latest_name = tokens[8]
latest_time = time
print(latest_name)
This is a rather fragile approach.
MDTM
A more reliable, but a way less efficient, is to use MDTM command to retrieve timestamps of individual files/folders:
names = ftp.nlst()
latest_time = None
latest_name = None
for name in names:
time = ftp.voidcmd("MDTM " + name)
if (latest_time is None) or (time > latest_time):
latest_name = name
latest_time = time
print(latest_name)
For an alternative version of the code, see the answer by #Paulo.
Non-standard -t switch
Some FTP servers support a proprietary non-standard -t switch for NLST (or LIST) command.
lines = ftp.nlst("-t")
latest_name = lines[-1]
See How to get files in FTP folder sorted by modification time.
Downloading found file
No matter what approach you use, once you have the latest_name, you download it as any other file:
with open(latest_name, 'wb') as f:
ftp.retrbinary('RETR '+ latest_name, f.write)
See also
Get the latest FTP folder name in Python
How to get FTP file's modify time using Python ftplib
Why don't you use next dir option?
ftp.dir('-t',data.append)
With this option the file listing is time ordered from newest to oldest. Then just retrieve the first file in the list to download it.
With NLST, like shown in Martin Prikryl's response,
you should use sorted method:
ftp = FTP(host="127.0.0.1", user="u",passwd="p")
ftp.cwd("/data")
file_name = sorted(ftp.nlst(), key=lambda x: ftp.voidcmd(f"MDTM {x}"))[-1]
If you have all the dates in time.struct_time (strptime will give you this) in a list then all you have to do is sort the list.
Here's an example :
#!/usr/bin/python
import time
dates = [
"Jan 16 18:35 2012",
"Aug 16 21:14 2012",
"Dec 05 22:27 2012",
"Jan 22 19:42 2012",
"Jan 24 00:49 2012",
"Dec 15 22:41 2012",
"Dec 13 01:41 2012",
"Dec 24 01:23 2012",
"Jan 21 00:35 2012",
"Jan 16 18:35 2012",
]
def main():
datelist = []
for date in dates:
date = time.strptime(date, '%b %d %H:%M %Y')
datelist.append(date)
print datelist
datelist.sort()
print datelist
if __name__ == '__main__':
main()
I don't know how it's your ftp, but your example was not working for me. I changed some lines related to the date sorting part:
import sys
from ftplib import FTP
import os
import socket
import time
# Connects to the ftp
ftp = FTP(ftpHost)
ftp.login(yourUserName,yourPassword)
data = []
datelist = []
filelist = []
ftp.dir(data.append)
for line in data:
col = line.split()
datestr = ' '.join(line.split()[5:8])
date = time.strptime(datestr, '%b %d %H:%M')
datelist.append(date)
filelist.append(col[8])
combo = zip(datelist,filelist)
who = dict(combo)
for key in sorted(who.iterkeys(), reverse=True):
print "%s: %s" % (key,who[key])
filename = who[key]
print "file to download is %s" % filename
try:
ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
except ftplib.err_perm:
print "Error: cannot read file %s" % filename
os.unlink(filename)
else:
print "***Downloaded*** %s " % filename
ftp.quit()

Convert dates to specific format

I have a pandas dataframe that looks like:
import pandas as pd
df1 = pd.DataFrame({'Counterparty':['Bank','Client','Bank','Bank','Bank','Bank'],
'Date':['4Q18','1Q19','2Q19','4Q21','FY22','H123']
})
I want to convert the 'Date' column from a string to a date such that the date is the last date for that particular period. ie 'FQ18'= 31st Dec 2018, '1Q19' = 31st Mar 2019, 'FY22' = 31st Dec 2022,'H123'= 30th June 2023
Any suggestions how to achieve this ?
As mentioned by #jpp, you're going to have to do some customization. There isn't existing functionality to map "FY22" to 2022-12-31, to my knowledge. Here's something to get you started, based on the limited example you've shown:
import re
import pandas as pd
from pandas.core.tools.datetimes import DateParseError
from pandas.tseries import offsets
halfyr = re.compile(r'H(?P<half>\d)(?P<year>\d{2})')
fiscalyr = re.compile(r'FY(?P<year>\d{2})')
def try_qend(date):
try:
return pd.to_datetime(date) + offsets.QuarterEnd()
except (DateParseError, ValueError):
halfyr_match = halfyr.match(date)
if halfyr_match:
half, year = [int(i) for i in halfyr_match.groups()]
month = 6 if half == 1 else 12
return pd.datetime(2000 + year, month, 1) + offsets.MonthEnd()
else:
fiscalyr_match = fiscalyr.match(date)
if fiscalyr_match:
year = int(fiscalyr_match.group('year'))
return pd.datetime(2000 + year, 12, 31)
else:
# You're SOL
return pd.NaT
def parse_dates(dates):
return pd.to_datetime([try_qend(date) for date in dates])
Assumptions:
All years are 20yy, not 19xx.
The regex patterns here completely describe the year-half/fiscal-year syntax set.
Example:
dates = ['4Q18','1Q19','2Q19','4Q21','FY22','H123']
parse_dates(dates)
DatetimeIndex(['2018-12-31', '2019-03-31', '2019-06-30', '2021-12-31',
'2022-12-31', '2023-06-30'],
dtype='datetime64[ns]', freq=None)

Groovy Script: List / Map Collection

I've created a script which I'm using to simulate the behaviour of a SOAP service in SOAP UI (as a mock service) for sprint testing purposes but am having problems when trying to iterate over a List I've created. The List is made up of a number of Maps, and each Map contains a List. It should look something like this:
[[Rec: 1, Items: ["AB123","BC234","CD345"]],[Rec: 2, Items: ["AB123","BC234","CD345","DE456"]]]
And this is the code I have to build up the List:
def offerMap = [:]
def outputList = []
def offerItemList = []
def outputMap = [:]
def outList = []
def item = ""
def rec = ""
offerItemList.add("AB123")
offerItemList.add("BC234")
offerItemList.add("CD345")
offerMap.put("Rec",1)
offerMap.put("Items",offerItemList)
outputList.add(offerMap)
log.info "OUT: outputList.size ${outputList.size()}"
log.info "OUT: offerItemList.size ${offerItemList.size()}"
offerMap.clear()
offerItemList.clear()
offerItemList.add("AB123")
offerItemList.add("BC234")
offerItemList.add("CD345")
offerItemList.add("DE456")
offerMap.put("Rec",2)
offerMap.put("Items",offerItemList)
outputList.add(offerMap)
log.info "OUT: outputList.size ${outputList.size()}"
log.info "OUT: offerItemList.size ${offerItemList.size()}"
And this is the the code I have to iterate over the list:
outputList.each {
log.info "OUT: outputList.size ${outputList.size()}"
outputMap.clear()
outputMap = it
rec = outputMap.get("Rec")
log.info "OUT: REC ${rec}"
outList.clear()
outList = outputMap.get("Items")
outList.each {
item = it
log.info "OUT: Item ${item}"
}
}
But this is not giving me the results I expect. The first problem is that the outputList.each loop appears to immediately be jumping to the second entry in the list, as witnessed from the output:
Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: outputList.size 1
Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: offerItemList.size 3
Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: outputList.size 2
Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: offerItemList.size 4
Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: outputList.size 2
Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: REC 2
Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: Item AB123
Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: Item BC234
Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: Item CD345
Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: Item DE456
Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: outputList.size 2
Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: REC null
I'm running out of ideas and fear I may be missing something fundamental due to my lack of experience with Groovy.
Consider the following. Note that the goal isn't entirely clear, but this is an educated guess (also unclear on Rec versus Row in your example at the top).
def outputList = [
['Rec': 1, 'Items': ["AB123","BC234","CD345"]],
['Rec': 2, 'Items': ["AB123","BC234","CD345","DE456"]]
]
outputList.each { outputMap ->
// is row == Rec ???
def row = outputMap['Rec']
println "OUT ROW: ${row}"
def items = outputMap['Items']
items.each { item ->
println "OUT Item: ${item}"
}
}
Output:
$ groovy Example.groovy
OUT ROW: 1
OUT Item: AB123
OUT Item: BC234
OUT Item: CD345
OUT ROW: 2
OUT Item: AB123
OUT Item: BC234
OUT Item: CD345
OUT Item: DE456

Resources