Related
if self.move_to == 'company':
self.employee_id.service_state = 'out_service'
# self. current_company.employee_id = self.env['hr.employee','res.company'].search([('company_id', '=', self.id)])
# self. current_company.employee_id.service_state = 'out_service'
self. current_company.employee_id .active = True
employee = self.env['hr.employee']
vals = {
'first_name': self.employee_id.first_name,
'second_name': self.employee_id.second_name,
'third_name': self.employee_id.third_name,
'last_name': self.employee_id.last_name,
'job_joining_date': self.employee_id.job_joining_date,
'company_id': self.employee_id.company_id.id,
'resource_calendar_id': self.employee_id.resource_calendar_id.id,
'tz': self.employee_id.tz,
'joining_date': self.employee_id.joining_date,
'name': self.employee_id.name,
'department_id': self.employee_id.department_id.id,
'gender': self.employee_id.gender,
'birthday': self.employee_id.birthday,
'employee_type': self.employee_id.employee_type,
'contract_type': employee.contract_type,
'first_employmment_date': self.employee_id.first_employmment_date,
'job_id': employee.job_id
}
employee.create(vals)
self.employee_id.write({'company_id': self.move_company,
'department_id': self.move_department.id,
'job_id': self.move_job.id,
'movement_date': fields.Date.today()})
this my code. I would like if the employee move to other company change employee state in current_company to out_service and archive the employee profile in current_company.
How can I do it?
i'm trying to get the pulse as an output for the given url using this code
from OTXv2 import OTXv2
from OTXv2 import IndicatorTypes
otx = OTXv2("my_key")
test=otx.get_indicator_details_full(IndicatorTypes.DOMAIN, "google.com")
and when i print test i become this output:
{'general': {'sections': ['general', 'geo', 'url_list', 'passive_dns', 'malware', 'whois', 'http_scans'], 'whois': 'http://whois.domaintools.com/google.com', 'alexa': 'http://www.alexa.com/siteinfo/google.com', 'indicator': 'google.com', 'type': 'domain', 'type_title': 'Domain', 'validation': [{'source': 'ad_network', 'message': 'Whitelisted ad network domain www-google-analytics.l.google.com', 'name': 'Whitelisted ad network domain'}, {'source': 'akamai', 'message': 'Akamai rank: #3', 'name': 'Akamai Popular Domain'}, {'source': 'alexa', 'message': 'Alexa rank: #1', 'name': 'Listed on Alexa'}, {'source': 'false_positive', 'message': 'Known False Positive', 'name': 'Known False Positive'}, {'source': 'majestic', 'message': 'Whitelisted domain google.com', 'name': 'Whitelisted domain'}, {'source': 'whitelist', 'message': 'Whitelisted domain google.com', 'name': 'Whitelisted domain'}], 'base_indicator': {'id': 12915, 'indicator': 'google.com', 'type': 'domain', 'title': '', 'description': '', 'content': '', 'access_type': 'public', 'access_reason': ''}, 'pulse_info': {'count': 0, 'pulses': [], 'references': [], 'related': {'alienvault': {'adversary': [], 'malware_families': [], 'industries': []}, 'other': {'adversary': [], 'malware_families': [], 'industries': []}}}, 'false_positive':...
i want to get only the part 'count': 0 in pulse_info
i tried using test.values() but it's like i have many dictionaries together
any idea how can i solve that?
Thank you
print(test["general"]["pulse_info"]["count"])
I have dictionary below
test = [ { 'id': '195', 'Name': 'i', 'Email': 'chdtn#gmail.com', 'role': 'Product' },
{ 'id': '219', 'Name': 'umar', 'Email': 'ddhi#gmail.com', 'role': 'Product' },
{ 'id': '74', 'Name': 'Are', 'Email': 'ddhit#gmail.com', 'role': 'Tester' },
{ 'id': '24', 'Name': 'Mee', 'Email': 'huul#gmail.com', 'role': 'Tester' },
{ 'id': '230', 'Name': 'abc', 'Email': 'deyan#gmail.com', 'role': 'Tester' },
{ 'id': '220', 'Name': 'Sc', 'Email': 'deyan#gmail.com', 'role': 'Product' },
{ 'id': '230', 'Name': 'Sn', 'Email': 'deyan#gmail.com', 'role': 'Tester' } ]
I need to extract unique email from above list dict
I need to give give role preference Product then to Tester
My Code is below
dict([(d['Email'], d) for d in test]).values()
My Out:
dict_values([{'id': '195', 'Name': 'i', 'Email': 'chdtn#gmail.com', 'role': 'Product'},
{'id': '219', 'Name': 'umar', 'Email': 'ddhi#gmail.com', 'role': 'Product'},
{'id': '74', 'Name': 'Are', 'Email': 'ddhit#gmail.com', 'role': 'Tester'},
{'id': '24', 'Name': 'Mee', 'Email': 'huul#gmail.com', 'role': 'Tester'},
{'id': '230', 'Name': 'Sn', 'Email': 'deyan#gmail.com', 'role': 'Tester'}])
Here in my out
{'id': '230', 'Name': 'Sn', 'Email': 'deyan#gmail.com', 'role': 'Tester'}
has to replace with
{ 'id': '220', 'Name': 'Sc', 'Email': 'deyan#gmail.com', 'role': 'Product' }
because "Product" have higher preference.
How to update my code? dict([(d['Email'], d) for d in test]).values()
Here is in case you would like to insist on using dictionaries.
We go from one row to another. Check if the email is already in the new dictionary as key.
If not, we add this as a new one.
If so, we check our new row. If our new role is "product", we will delete what was already in the dictionary, and add the new row.
new_dict = {}
for row in test:
if row["Email"] not in new_dict.keys():
new_dict.update({row["Email"]: row})
else:
if row["role"]=="Product":
new_dict.pop(row["Email"])
new_dict.update({row["Email"]: row})
Perhaps you could try it with two loops; once to get the unique emails, and second time to make sure to prioritize "Product".
It wasn't clear what happens if there is no "Product" for duplicate "Emails", so in the loop below, the first email is selected in that case.
tmp = {}
for d in test:
tmp.setdefault(d['Email'], []).append(d)
out = []
for k, lst in tmp.items():
if len(lst) == 1:
out.append(lst[0])
else:
for d in lst:
if d['role'] == 'Product':
out.append(d)
break
else:
out.append(lst[0])
Output:
[{'id': '195', 'Name': 'i', 'Email': 'chdtn#gmail.com', 'Account': 'Product'},
{'id': '219', 'Name': 'umar', 'Email': 'ddhi#gmail.com', 'Account': 'Product'},
{'id': '74', 'Name': 'Are', 'Email': 'ddhit#gmail.com', 'role': 'Tester'},
{'id': '24', 'Name': 'Mee', 'Email': 'huul#gmail.com', 'role': 'Tester'},
{'id': '220', 'Name': 'Sc', 'Email': 'deyan#gmail.com', 'role': 'Product'}]
Make it to a data frame and drop_duplicates by Email after sorting the column role.
test = [ { 'id': '195', 'Name': 'i', 'Email': 'chdtn#gmail.com', 'role': 'Product' },
{ 'id': '219', 'Name': 'umar', 'Email': 'ddhi#gmail.com', 'role': 'Product' },
{ 'id': '74', 'Name': 'Are', 'Email': 'ddhit#gmail.com', 'role': 'Tester' },
{ 'id': '24', 'Name': 'Mee', 'Email': 'huul#gmail.com', 'role': 'Tester' },
{ 'id': '230', 'Name': 'abc', 'Email': 'deyan#gmail.com', 'role': 'Tester' },
{ 'id': '220', 'Name': 'Sc', 'Email': 'deyan#gmail.com', 'role': 'Product' },
{ 'id': '230', 'Name': 'Sn', 'Email': 'deyan#gmail.com', 'role': 'Tester' } ]
df = pd.DataFrame(test)
df1 = df.sort_values(by = ["Email", "role"], ascending = True)
res_df = df1.drop_duplicates(["Email"])
output_list = []
for i in res_df.values :
output_list.append(dict([("id", i[0]), ("Name", i[1]), ("Email", i[2]), ("role", i[3])]))
> output_list
[{'id': '195', 'Name': 'i', 'Email': 'chdtn#gmail.com', 'role': 'Product'},
{'id': '219', 'Name': 'umar', 'Email': 'ddhi#gmail.com', 'role': 'Product'},
{'id': '74', 'Name': 'Are', 'Email': 'ddhit#gmail.com', 'role': 'Tester'},
{'id': '220', 'Name': 'Sc', 'Email': 'deyan#gmail.com', 'role': 'Product'},
{'id': '24', 'Name': 'Mee', 'Email': 'huul#gmail.com', 'role': 'Tester'}]
Context:
We have a requirement where I need to capture a specific transaction using KafkaConsumer library.I use a unique session as key which I obtain from a different tool(let's call as Feeder tool) to capture the transaction.
I run my code immediately once the session is obtained from Feeder.
Problem
I am able to fetch multiple records from Kafka but I don't see the record which I'm trying to filter using the session.
Code
from kafka import KafkaConsumer
import json
SESSION = 'sessionID'
def consumeRecords(topic,group,bootstrapserver,auto_offset_reset,mySession,auto_commit_boolean):
consumer = KafkaConsumer(topic,group_id=group,bootstrap_servers=bootstrapserver,auto_offset_reset=auto_offset_reset,enable_auto_commit=auto_commit_boolean)
consumer.topics()
consumer.seek_to_beginning()
try:
while True:
print("CALLING POLL")
records = consumer.poll(timeout_ms=1000)
print("RETURNED FROM POLL")
if records:
for consumedRecord in records.values():
for msg in consumedRecord:
json_data = json.loads(msg.value.decode('utf-8'))
#print(json_data)
if 'alias' in json_data.keys() and json_data['alias']=='myServer':
current_session = json_data[SESSION]
print("SESSION is :" , current_session)
if mySession == current_session :
print('My record is ', json_data)
except Exception as e:
print("Unable to find any related sessions")
print(e)
if __name__ == '__main__':
KAFKA_TOPIC = 'e-commerce.request'
KAFKA_GROUP = 'test'
KAFKA_BROKERS = ['ABC.net:9092', 'DEF:9092']
auto_commit = False
consumeRecords(KAFKA_TOPIC,KAFKA_GROUP,KAFKA_BROKERS,'earliest','38l87jondkvnefNW886QMTWVcN6S4my5Y-No167ZzqF',auto_commit)
I'm supposed to print the following json data consumed from Kafka , but my code doesn't fetch this record and hence prints nothing and runs for infinite time
{'Type': 'request', 'requestID': '2018100819564659-5', 'payload': {'timing': {'startTime': '20181008195624322', 'total': '0.063', 'totalActions': '0', 'jsp': '0.063'}, 'user': {'orgID': '', 'userID': '', 'newComer': 'FALSE', 'helpdeskUserID': '', 'helpdeskUserOrgID': '', 'travelerID': ''}, 'version': '1.0.0', 'client': {'referrer': '', 'ip': ''}, 'url': {'parameters': {'JSESSIONID': '38l87jondkvnefNW886QMTWVcN6S4my5Y-No167ZzqF!430553578!-1652153437'}, 'baseUrl': 'http://server_url', 'path': 'DUMMY', 'method': 'POST'}, 'actions': [{'cumulTiming': '0', 'name': 'OverrideServlet', 'isChained': 'FALSE', 'features': '[GlobalTimeSpent = 0][PRE_RULES = 0][POST_RULES = 0]', 'chainParent': ''}], 'context': {'sessionSize': '12|12', 'fatalError': 'FALSE', 'requestType': 'XML', 'error': [], 'requestedSessionID': '', 'templateName': ''}}, 'Hostname': 'DummyAgain', 'sessionID': '38l87jondkvnefNW886QMTWVcN6S4my5Y-No167ZzqF', 'Site': 'ABCDEFGH', 'ClientId': 1234551515439, 'Epoch': 1539028584353, 'IP': 'A.B.C.D', 'alias': 'myServer', 'SeqNb': 21845, 'Source': 'eCommerce'}
I am trying to convert a semicolon delimited file to a nested dict. Been working on this quite a bit this morning and guessing I am overlooking something simple:
Input (Sample)
This is actually about 200 lines long. Just a small sample.
key;name;desc;category;type;action;range;duration;skill;strain_mod;apt_bonus
ambiencesense;Ambience Sense;This sleight provides the async with an instinctive sense about an area and any potential threats nearby. The async receives a +10 modifier to all Investigation, Perception, Scrounging, and Surprise Tests.;psi-chi;passive;automatic;self;constant;;0;
cogboost;Cognitive Boost;The async can temporarily elevate their cognitive performance.;psi-chi;active;quick;self;temp;;-1;{'COG': 5}
Current Output
[['key',
'name',
'desc',
'category',
'type',
'action',
'range',
'duration',
'skill',
'strain_mod',
'apt_bonus'],
['ambiencesense',
'Ambience Sense',
'This sleight provides the async with an instinctive sense about an area and any potential threats nearby. The async receives a +10 modifier to all Investigation, Perception, Scrounging, and Surprise Tests.',
'psi-chi',
'passive',
'automatic',
'self',
'constant',
'',
'0',
''],
['cogboost',
'Cognitive Boost',
'The async can temporarily elevate their cognitive performance.',
'psi-chi',
'active',
'quick',
'self',
'temp',
'',
'-1',
"{'COG': 5}"]]
Desired Output
blahblah = {
'ambiencesense': {
'name': 'Ambiance Sense'
'desc': 'This sleight provides the async with an instinctive sense about an area and any potential threats nearby. The async receives a +10 modifier to all Investigation, Perception, Scrounging, and Surprise Tests.',
'category': 'psi-chi',
'type': 'passive',
'action': 'automatic',
'range': 'self',
'duration': 'constant',
'skill': '',
'strain_mod': '0',
'apt_bonus': '',
},
'cogboost': {
'name': 'Cognitive Boost'
'desc': 'The async can temporarily elevate their cognitive performance.',
'category': 'psi-chi',
'type': 'active',
'action': 'quick',
'range': 'self',
'duration': 'temp',
'skill': '',
'strain_mod': '-1',
'apt_bonus': 'COG', 5',
},
...
Script (Nonfunctional)
#!/usr/bin/env python
# Usage: ./csvdict.py <filename to convert to dict> <file to output>
import csv
import sys
import pprint
def parse(filename):
with open(filename, 'rb') as csvfile:
dialect = csv.Sniffer().sniff(csvfile.read(), delimiters=';')
csvfile.seek(0)
reader = csv.reader(csvfile, dialect)
dict_list = []
for line in reader:
dict_list.append(line)
return dict_list
new_dict = {}
for item in dict_list:
key = item.pop('key')
new_dict[key] = item
output = parse(sys.argv[1])
with open(sys.argv[2], 'wt') as out:
pprint.pprint(output, stream=out)
Working Script
#!/usr/bin/env python
# Usage: ./csvdict.py <input filename> <output filename>
import sys
import pprint
file_name = sys.argv[1]
data = {}
error = 'Incorrect number of arguments.\nUsage: ./csvdict.py <input filename> <output filename>'
if len(sys.argv) != 3:
print(error)
else:
with open(file_name, 'r') as test_fh:
header_line = next(test_fh)
header_line = header_line.strip()
headers = header_line.split(';')
index_headers = {index:header for index, header in enumerate(headers)}
for line in test_fh:
line = line.strip()
values = line.split(';')
index_vals = {index:val for index, val in enumerate(values)}
data[index_vals[0]] = {index_headers[key]:value for key, value in index_vals.items() if key != 0}
with open(sys.argv[2], 'wt') as out:
pprint.pprint(data, stream=out)
The only thing this doesn't handle well is the embedded dicts. Any ideas how to clean this up? (see apt_bonus)
'cogboost': {'action': 'quick',
'apt_bonus': "{'COG': 5}",
'category': 'psi-chi',
'desc': 'The async can temporarily elevate their cognitive performance.',
'duration': 'temp',
'name': 'Cognitive Boost',
'range': 'self',
'skill': '',
'strain_mod': '-1',
'type': 'active'},
Here's another version that is a bit more drawn out, but has no dependancies.
file_name = "<path>/test.txt"
data = {}
with open(file_name, 'r') as test_fh:
header_line = next(test_fh)
header_line = header_line.strip()
headers = header_line.split(';')
index_headers = {index:header for index, header in enumerate(headers)}
for line in test_fh:
line = line.strip()
values = line.split(';')
index_vals = {index:val for index, val in enumerate(values)}
data[index_vals[0]] = {index_headers[key]:value for key, value in index_vals.items() if key != 0}
print(data)
This is pretty easy to do with pandas:
In [7]: import pandas as pd
In [8]: pd.read_clipboard(sep=";", index_col=0).T.to_dict()
Out[8]:
{'ambiencesense': {'action': 'automatic',
'apt_bonus': nan,
'category': 'psi-chi',
'desc': 'This sleight provides the async with an instinctive sense about an area and any potential threats nearby. The async receives a +10 modifier to all Investigation, Perception, Scrounging, and Surprise Tests.',
'duration': 'constant',
'name': 'Ambience Sense',
'range': 'self',
'skill': nan,
'strain_mod': 0,
'type': 'passive'},
'cogboost': {'action': 'quick',
'apt_bonus': "{'COG': 5}",
'category': 'psi-chi',
'desc': 'The async can temporarily elevate their cognitive performance.',
'duration': 'temp',
'name': 'Cognitive Boost',
'range': 'self',
'skill': nan,
'strain_mod': -1,
'type': 'active'}}
In your case, you'd use pd.read_csv() instead of .read_clipboard() but it would look roughly the same. You might also need to tweak it a little if you want to parse the apt_bonus column as a dictionary.
Try this pythonic way using no libraries:
s = '''key;name;desc;category;type;action;range;duration;skill;strain_mod;apt_bonus
ambiencesense;Ambience Sense;This sleight provides the async with an instinctive sense about an area and any potential threats nearby. The async receives a +10 modifier to all Investigation, Perception, Scrounging, and Surprise Tests.;psi-chi;passive;automatic;self;constant;;0;
cogboost;Cognitive Boost;The async can temporarily elevate their cognitive performance.;psi-chi;active;quick;self;temp;;-1;{'COG': 5}'''
lists = [delim.split(';') for delim in s.split('\n')]
keyIndex = lists[0].index('key')
nested = {lst[keyIndex]:{lists[0][i]:lst[i] for i in range(len(lists[0])) if i != keyIndex} for lst in lists[1:]}
That results with:
{
'cogboost': {
'category': 'psi-chi',
'name': 'Cognitive Boost',
'strain_mod': '-1',
'duration': 'temp',
'range': 'self',
'apt_bonus': "{'COG': 5}",
'action': 'quick',
'skill': '',
'type': 'active',
'desc': 'The async can temporarily elevate their cognitive performance.'
},
'ambiencesense': {
'category': 'psi-chi',
'name': 'Ambience Sense',
'strain_mod': '0',
'duration': 'constant',
'range': 'self',
'apt_bonus': '',
'action': 'automatic',
'skill': '',
'type': 'passive',
'desc': 'This sleight provides the async with an instinctive sense about an area and any potential threats nearby. The async receives a +10 modifier to all Investigation, Perception, Scrounging, and Surprise Tests.'
}
}