Using a Wildcard after a Like when using params.has_key - ruby-on-rails-4

I have a search field filter on a docket_id or address like with wildcards before and after, on an orders table as below:-
if params.has_key?("docket_id_filter")
#orders = #orders.where("docket_id like ? OR address like ?", "%#{params[:docket_id_filter]}%", "%#{params[:docket_id_filter]}%")
end
I have a problem when users copy and paste a value in the search field with a "carriage return" or space after the number.
This returns no values as it doesn't match the criteria.
The code running on Rails for a correct search is as below:-
Started GET "/orders?utf8=%E2%9C%93&docket_id_filter=6140132790&state_filter=All&day_filter%5Bday%5D=&commit=Filter" for 127.0.0.1 at 2015-02-09 16:09:22 +0800
while one with an space after it, is as below:-
Started GET "/orders?utf8=%E2%9C%93&docket_id_filter=6140099196 &state_filter=All&day_filter%5Bday%5D=&commit=Filter" for 127.0.0.1 at 2015-02-09 16:13:45 +0800
How can I do a check to see the last character to exclude it? Do i use a ends_with or is there a way to ignore it or format the search field before running the code.
or
Can i check the last character and give an error pop-up msg?
Thanks in advance

Solved it! :-)
if params.has_key?("docket_id_filter")
if "docket_id_filter".last(0) == ''
#orders = #orders.where("docket_id like ?", "%#{params[:docket_id_filter].first(10)}%")
else
#orders = #orders.where("docket_id like ? OR address like ?", "%#{params[:docket_id_filter]}%", "%#{params[:docket_id_filter]}%")
end
end

Related

Exact match of string in pandas python

I have a column in data frame which ex df:
A
0 Good to 1. Good communication EI : tathagata.kar#ae.com
1 SAP ECC Project System EI: ram.vaddadi#ae.com
2 EI : ravikumar.swarna Role:SSE Minimum Skill
I have a list of of strings
ls=['tathagata.kar#ae.com','a.kar#ae.com']
Now if i want to filter out
for i in range(len(ls)):
df1=df[df['A'].str.contains(ls[i])
if len(df1.columns!=0):
print ls[i]
I get the output
tathagata.kar#ae.com
a.kar#ae.com
But I need only tathagata.kar#ae.com
How Can It be achieved?
As you can see I've tried str.contains But I need something for extact match
You could simply use ==
string_a == string_b
It should return True if the two strings are equal. But this does not solve your issue.
Edit 2: You should use len(df1.index) instead of len(df1.columns). Indeed, len(df1.columns) will give you the number of columns, and not the number of rows.
Edit 3: After reading your second post, I've understood your problem. The solution you propose could lead to some errors.
For instance, if you have:
ls=['tathagata.kar#ae.com','a.kar#ae.com', 'tathagata.kar#ae.co']
the first and the third element will match str.contains(r'(?:\s|^|Ei:|EI:|EI-)'+ls[i])
And this is an unwanted behaviour.
You could add a check on the end of the string: str.contains(r'(?:\s|^|Ei:|EI:|EI-)'+ls[i]+r'(?:\s|$)')
Like this:
for i in range(len(ls)):
df1 = df[df['A'].str.contains(r'(?:\s|^|Ei:|EI:|EI-)'+ls[i]+r'(?:\s|$)')]
if len(df1.index != 0):
print (ls[i])
(Remove parenthesis in the "print" if you use python 2.7)
Thanks for the help. But seems like I found a solution that is working as of now.
Must use str.contains(r'(?:\s|^|Ei:|EI:|EI-)'+ls[i])
This seems to solve the problem.
Although thanks to #IsaacDj for his help.
Why not just use:
df1 = df[df['A'].[str.match][1](ls[i])
It's the equivalent of regex match.

How to manage looping on this list on Applscript?

The list is in the form of:-
0:
https://url
1:
https://url
..... And so on.
How could I loop on this list. So I could fetch the number first without ":" and type it somewhere then fetch the url that comes after that number and type it elsewhere. Then end repeat if the list is over.
Or should I use records instead?
I am still a beginner using AppleScript. I tried many commands I mixed up but the computer keeps running the script nonestop and the activity monitor shows the applescript using 100% of the processor and huge amount of ram.
Appreciate any help.
Thank you
You didn't define what your list really looks like very well so I made an assumption on my answer below. If I was wrong, hopefully my answer will at least point you in the right direction. (or if I've gotten it wrong, but you can choose to reformat it to the way I suggested, that could still help)
on run
set theList to {"0:http://apple.com", "1:http://google.com"} -- my guess at what your list looks like.
repeat with anItem in theList
set anItem to anItem as string
set itemParts to myParseItem(anItem)
set tID to the_integer of itemParts as integer
set tURL to the_url of itemParts as string
end repeat
end run
on myParseItem(theItem)
set AppleScript's text item delimiters to ":"
set delimitedList to every text item of theItem
set newString to (items 2 thru -1 of delimitedList as string)
set AppleScript's text item delimiters to {""}
set theInt to item 1 of delimitedList
set theURL to newString as string
return {the_integer:theInt, the_url:theURL}
end myParseItem

AWQL - how can i use a regular expressions or something similar?

I am querying the adwords api via the following AWQL-Query (which works fine):
SELECT AccountDescriptiveName, CampaignId, CampaignName, AdGroupId, AdGroupName, KeywordText, KeywordMatchType, MaxCpc, Impressions, Clicks, Cost, Conversions, ConversionsManyPerClick, ConversionValue
FROM KEYWORDS_PERFORMANCE_REPORT
WHERE CampaignStatus IN ['ACTIVE', 'PAUSED']
AND AdGroupStatus IN ['ENABLED', 'PAUSED']
AND Status IN ['ACTIVE', 'PAUSED']
AND AdNetworkType1 IN ['SEARCH'] AND Impressions > 0
DURING 20140501,20140531
Now i want to exclude some campaigns:
we have a convention for our new campaigns that the campaign name begins with three numbers followed by an underscore, eg. "100_brand_all"
So i want to get only these new campaigns..
I tried lots of different variations for STARTS_WITH but only exact strings are working - but i need a pattern to match!
I already read https://developers.google.com/adwords/api/docs/guides/awql?hl=en and following its content it should be possible to use a WHERE expression like this:
CampaignName STARTS_WITH ['0','1','2','3']
But that doesn't work!
Any other ideas how i can achieve this?
Well, why don't you run a campaign performance report first, then process that ( get the campaign ids you want or don't want) the use those in the "CampaignId IN [campaign ids here] . or CampaignID NOT_IN [campaign ids]

Django Query in a loop fails for no good reason

I have this code:
msgs = int(post['time_in_weeks'])
for i in range(msgs):
tip_msg = Tip.objects.get(week_number=i)
it always results in an error saying that no values could be found.
week_number is an integer field. When I input the value of i directly,
the query works.
When i print out the value of i I get the expected values.
Any input at all would be seriously appreciated.
Thanks.
The range function will give you a zero based list of numbers up to and excluding msgs. I guess there is no Tip with week_number=0.
Also, to limit the number of queries you could do this:
for tip in Tip.objects.filter(week_number__lt=msgs):
#do something
or, if you want specific weeks:
weeks=(1,3,5)
for tip in Tip.objects.filter(week_number__in=weeks):
#do something

Django Filtering Problem

I'm trying to set up a filter query in one of my views...basically my code looks as below:
def inventory(request):
vehicle = Vehicle.objects.all().exclude(status__status='Incoming').order_by('common_vehicle__series__model__manufacturer__manufacturer', 'common_vehicle__series__model__model', 'common_vehicle__year')
year_count = Vehicle.objects.exclude(status__status='Incoming').order_by('-common_vehicle__year__year').values('common_vehicle__year__year').annotate(count=Count('id'))
make_count = Vehicle.objects.exclude(status__status='Incoming').order_by('common_vehicle__series__model__manufacturer__manufacturer').values('common_vehicle__series__model__manufacturer__manufacturer').annotate(count=Count('id'))
return render_to_response('vehicles.html', {'vehicle': vehicle, 'make_count': make_count, 'year_count': year_count,})
def year_filter(request, year):
vehicle = Vehicle.objects.filter(common_vehicle__year__year=year)
return render_to_response('filter.html', {'vehicle':vehicle,})
def make_filter(request, make):
vehicle = Vehicle.objects.filter(common_vehicle__series__model__manufacturer__manufacturer=make).exclude(status__status='Incoming')
return render_to_response('filter.html', {'vehicle':vehicle,})
So far when I try any of the last two views, I'm only getting the query set from the first view i.e. inventory. The URLConf file looks as below:
(r'^inventory/year/(?P<year>d{4})/?$', 'app.vehicles.views.year_filter'),
(r'^inventory/make/(?P<make>)/', 'app.vehicles.views.make_filter'),
It looks to me like you're missing an expression.
(r'^inventory/year/(?P<year>d{4})/?$', 'app.vehicles.views.year_filter'),
(r'^inventory/make/(?P<make>)/', 'app.vehicles.views.make_filter'),
<year> matches a series of 4 digits, what does <make> match?
(r'^inventory/make/(?P<make>[-\w]+)/', 'app.vehicles.views.make_filter'),
That matches something to the make variable. Happy to know if I'm wrong!
It looks like in your URLConf, above the ones you have already mentioned, you might have a mapping which overrides the ones below -
say like -
(r'^inventory/', 'app.vehicles.views.inventory'),
which is causing all the calls in inventory to hit the method inventory.
This is because Django goes through it in a serial fashion. Any URL which matches it first will hit it.
To overcome this, you reorder the urls in the following way -
(r'^inventory/year/(?P<year>d{4})/?$', 'app.vehicles.views.year_filter'),
(r'^inventory/make/(?P<make>)/', 'app.vehicles.views.make_filter'),
(r'^inventory/', 'app.vehicles.views.inventory'),
Koran is on the right path. Try having this in your URLconf:
(r'^inventory/$', 'app.vehicles.views.inventory'),
(r'^inventory/year/(?P<year>d{4})/?$', 'app.vehicles.views.year_filter'),
(r'^inventory/make/(?P<make>)/', 'app.vehicles.views.make_filter'),
The regex r'^inventory/$' matches strictly just that, nothing past the '/'. This should cause the other URLs to map to the proper views.
d{4} always matches exactly four d's, dddd. Is that what you want?
(r'^inventory/year/(?P<year>d{4})/?$', 'app.vehicles.views.year_filter'),
I suspect you need to escape d with \, compare:
(r'^inventory/year/(?P<year>\d{4})/?$', 'app.vehicles.views.year_filter'),