Skip to main content

Some Python snippets -I

Some python snippets that can be useful. I have come up with some requirements in my scripts and searched for the solutions. The working ones are kept here for reference.

Counting based on condition of dictionary values.

Lets say we have a dictionary with some keys and values like this:

{
  'a' : 'hello',
  'b' : 'world,
  'c' : 'hello',
  'd' : 'junk'
}





Now if you want to count number of keys that has values as "hello"
Here is the snippet for it

count = sum(1 for x in dict.values() if x =='hello')

This will display the count as 2.


Returning keys of dictionary of sorted values.

Sometimes we have to sort the dictionary with values. I have to get the list of keys in sorted order of values.
This snipped does exactly that:

sortedKeyList = sorted(dict,key=dict.get, reverse=True)

the sortedKeyList is the list of keys of dictionary where the keys are sorted based on values of the dictionary

Comments

Popular posts from this blog

Verdi Uses - I

COVERAGE While going through some of the blog posts, i found that the Verdi supports even coverage right out of the box, but from the 2014 Version. Well till now, we relied on the URG reports and the DVE to review the coverage and apply waivers and create exclusion files. Looks verdi supports all the features.. I did not get any hands on the features of this option, soon i will find some time to play around with this option of coverage in Verdi. How to launch the verdi with coverage. $> verdi -cov -covdir <PATH_TO_TB>.simv.vdb -covdir <PATH_TO_TEST1>.simv.vdb -covdir <PATH_TO_TEST2>.simv.vdb...  You can pass multiple covdirs, where in all the results will be merged. References: SYNOPSYS SITE Think Verification  

Virtual Sequence and Virtual Sequencer in UVM

Virtual sequences and sequencers in UVM are just virtual containers of multiple sequences and sequencers. Using virtual sequencers and sequences can be done in these three ways: Using only virtual Sequence and handles of sequencers inside the virtual sequence. Using Virtual sequencer to hold the sequencers Same as step 2, by using uvm_declare_p_sequencer. Virtual Sequencer does not create any sequencer objects, rather it points to other physical sequencers in different agents.  Virtual Sequencer extends from uvm_sequencer. Virtual sequence will create its sequences and start them. Running sequences can be controlled by user in the body() task. virtual sequence extends from uvm_sequence. METHOD-1: In this method, we will have one base virtual sequence which will hold the handles to different sequencers. Here we dont use any virtual sequencer. We just use the nested seqeunces with handles ot sequencers inside sequence itself. //This is the base virtual se...

Good uses of System Verilog DPI..

What is DPI?? Well, The easiest answer is Direct Programming Interface. The DPI is used to communicate the system verilog/ Verilog code to any other language. Well DPI has its own advantages over PLI.  You can check out the differences between the DPI and PLI   HERE . With DPI, you can directly call the c-functions from system verilog code and vice versa. With lot of SOCs and complicated chips in the silicon industry, sometimes you cannot completely live with the generic UVM/System verilog to write complete stimulus for complete chip.  Basics of DPI, Basics of DPI can be found when you google it.  There are some interesting cases where you will find the uses of DPI. I will get through some advanced take aways from IEEE 1800.2012 LRM. You cannot export the objects with in class. You can export only from static objects. exporting the tasks/functions should be in a module/program/interface scope.[or any static scope] . ..... Bear with me as th...