A simple decorator written as a class, which counts how many times a function has been called

Can I improve its typing? Is there any other improvement or pythonic change that you would do?

F = TypeVar('F', bound=Callable[..., Any])   # This is mostly so that I practice using a class as a decorator. class CountCalls:     """Logs to DEBUG how many times a function gets called, saves the result in a newly created attribute `num_calls`."""     def __init__(self, func: F) -> None:         functools.update_wrapper(self, func)         self.func = func         self.num_calls: int = 0         self._logger = logging.getLogger(__name__ + '.' + self.func.__name__)         self.last_return_value = None      def __call__(self, *args: Any, **kwargs: Any) -> Any:         self.num_calls += 1         self._logger.debug(' called %s times', self.num_calls)         self.last_return_value = self.func(*args, **kwargs)         return self.last_return_value 

Here’s the decorator in action:

@CountCalls def asdf(var: str):     print(var)     return len(var) asdf(5) Traceback (most recent call last):   File "<input>", line 1, in <module>   File "C:/Projects/Python/he/src/he/decorators.py", line 139, in __call__     self.last_return_value = self.func(*args, **kwargs)   File "<input>", line 4, in asdf TypeError: object of type 'int' has no len() 5 asdf('Laur') Laur 4 asdf.last_return_value 4 asdf('fake gangsta') fake gangsta 12 

Can’t get counts back from large table in Postgres

I have a table with 1.2 billion rows in it. The table has 60-70 columns, the vast majority of which are empty.

The table is indexed on subscription_id, among other indexes.

My query is:

SELECT count(distinct listing_id) FROM "apps" WHERE "apps"."subscription_id" = 1298 

This particular subscription_id is going to have roughly 30M rows to count. Unfortunately I am unable to do so as the query takes in excess of one hour just to get that count.

I know there must be some way to improve it. Would it be improved if I created an index on subscription_id and listing_id together?

Representation of Subgraph Counts using Polynomial of Adjacency Matrix

We consider a graph $ G$ of size $ d$ with adjacency matrix $ A$ , whose entries take value in $ \{0,1\}$ . We are interested in the number of a certain connected subgraph $ S$ of size $ k$ in $ G$ . For example, the subgraph $ S$ consists of edges $ (1,2),(2,3),(3,4),(4,2)$ up to relabeling of the $ k = 4$ vertices $ 1,2,3,4$ . We can also view this subgraph as a path of length $ k = 4$ from vertex $ 1$ to vertex $ 2$ . Here we allow repeated vertices in the path, for example, vertex $ 2$ appears twice.

Question: Is the following claim true?

Let $ s$ denote the number of subgraphs $ S$ in $ G$ . For any graph $ G$ and subgraph $ S$ , there exist $ \{\Theta^{(i)}\}_{i=1}^k$ that $ $ s = \sum_{i = 1}^k\langle \Theta^{(i)}, A^i \rangle, $ $ where $ \Theta^{(i)} \in \mathbb{R}^{d\times d}$ is the coefficient of the $ i$ -th order polynomial of the adjacency matrix $ A$ , and $ k$ is the size of the subgraph of interest.

Script that counts quarters, dimes, nickels, and pennies

I am learning python and I wrote a script that counts how many coins you would need for an amount in dollars. I was wondering if I could make any improvements to it.

def change():     amnt = float(input("Enter an amount in USD: "))     quarters = divmod(amnt, 0.25)     print("Quarters: ", quarters[0])     amnt = round(quarters[1], 2)     dimes = divmod(amnt, 0.10)     print("Dimes: ", dimes[0])     amnt = round(dimes[1], 2)     nickels = divmod(amnt, 0.     print("Nickels: ", nickels[0])     amnt = round(nickels[1], 2)     penny = divmod(amnt, 0.01)     print("Pennies", penny[0]) change() 

Trying to Get Average of a Column in SharePoint – But One Row May Contain Multiple Counts

Ok so getting the average means dividing the sum by the count. Meaning I have a column with a count of 7 rows of numbers that all add up to the sum of 10. So I divide 7/10 to get 70%. Using a combination of calculated columns and workflows I am able to spit out the average number of a column.

HOWEVER, this one column has a slight twist, and I feel there’s a mathematical formula someone can give me to figure it out, but I can’t get it.

The issue is one of my single rows may represent up to two counts. The way this form was setup is a user can essentially enter two forms’ worth of info into one form. I have to parse out when they’ve entered only one form or two forms’ worth of info and if they’ve entered more than one form, I have to somehow add that to the count total to get an accurate average.

Right now I can create a calculated column to parse out whether there’s one or two forms entered and spit out a number 1 or 2 respectively. However, at that point I don’t know how to adjust the count to get an accurate average. Can anyone help? Thanks so much!

To clarify further, here’s a basic example of how my column turns out:

FORM COLUMN 1 1 1 0 2 1 1 —- Average: 100%

See that my column spits out an average of 100% because it’s dividing 7 by 7. However, the row item that states the number 2 is actually trying to say that’s two rows of info, so I need it to calculate the average as if the column looked like his:

FORM COLUMN ACCURATE 1 1 1 0 1 (split from the 2 items) 1 (split from the 2 items) 1 1 —- Accurate average: 87.5%

See how when you account the 2 as a COUNT of 2, you get the accurate average because it’s 8 items, but 7 are accurate so we get 87.5% accuracy.

Let me know if I need more clarification but hopefully someone can help me figure this out. Thanks so much!

Python Pandas NLTK: Adding Frequency Counts or Importance Scoring to Part of Speech Chunks on Dataframe Text Column

I did NLTK part of speech tagging followed by chunking on one column (“train_text”) inside my Pandas data frame.

Below is my code that ran successfully and sample output results.

def process_content():     try:         for i in train_text:             words = nltk.word_tokenize(i)             tagged = nltk.pos_tag(words)             # chunkGram = r"""Chunk: {<RB.?>*<VB.?>*<NNP>+<NN>?}"""             chunkGram = r"""Chunk: {<VB.?><NN.?>}"""             chunkParser = nltk.RegexpParser(chunkGram)             chunked = chunkParser.parse(tagged)              for subtree in chunked.subtrees(filter = lambda t: t.label() == 'Chunk'):                 print (subtree)      except Exception as e:         print(str(e))  process_content() 

Results: “xxx” stands for a word; in the first instance it is a verb and in the second instance it is a noun

(Chunk xxx/VBN xxx/NN)  (Chunk xxx/VBN xxx/NN)  (Chunk xxx/VBN xxx/NN)  (Chunk xxx/VBN xxx/NN)  (Chunk xxx/VBN xxx/NN)  

Now that I have the chunks of words, I want to find the 10 most frequently occurring or prominent Verb + Noun chunks. Is there any way I can attach a frequency or importance score to each chunk?

Python Pandas NLTK: Adding Frequency Counts or Importance Scoring to Part of Speech Chunks on Dataframe Text Column

I did NLTK part of speech tagging followed by chunking on one column (“train_text”) inside my Pandas data frame.

Below is my code that ran successfully and sample output results.

def process_content():     try:         for i in train_text:             words = nltk.word_tokenize(i)             tagged = nltk.pos_tag(words)             # chunkGram = r"""Chunk: {<RB.?>*<VB.?>*<NNP>+<NN>?}"""             chunkGram = r"""Chunk: {<VB.?><NN.?>}"""             chunkParser = nltk.RegexpParser(chunkGram)             chunked = chunkParser.parse(tagged)              for subtree in chunked.subtrees(filter = lambda t: t.label() == 'Chunk'):                 print (subtree)      except Exception as e:         print(str(e))  process_content() 

Results: “xxx” stands for a word; in the first instance it is a verb and in the second instance it is a noun

(Chunk xxx/VBN xxx/NN)  (Chunk xxx/VBN xxx/NN)  (Chunk xxx/VBN xxx/NN)  (Chunk xxx/VBN xxx/NN)  (Chunk xxx/VBN xxx/NN)  

Now that I have the chunks of words, I want to find the 10 most frequently occurring or prominent Verb + Noun chunks. Is there any way I can attach a frequency or importance score to each chunk?