Counter in Collections module Python - collections

I've come across a really weird problem. I'm trying to use Counter function in collections module. However, I keep getting the same error message
AttributeError: 'module' object has no attribute 'Counter'
I have tried using it before and it worked fine, but now for some reason when I import "collections" module it has a very limited number of attributes.
I have tried:
import collections # when calling Counter I would then use collections.Counter()
import collections as collect # collect.Counter()
For both of those I keep getting Attribute Error.
I have also tried
from collections import Counter
And in this case I got:
ImportError: cannot import name Counter
These are all tested both in ipython interface and through a script (not importing anything else, just the collections).
Any ideas?

The Counter class was added to the module in Python 2.7. You are most likely using Python 2.6 or older. From the collections.Counter() documentation:
New in version 2.7.
On python 2.5 or 2.6, use this backport instead.

Came across the same issue while installing pandas.
Cause: Counter is only supported in python2.7 and higher and is not available in earlier versions - Counter class got added into collections package in Python 2.7.
Solution 1: As stated by Martin Pieters - use the backport.
Add counter.py at /lib64/python2.6/ - this is where the collections.py is ./lib64/python2.6/collections.py
Patch collections.py with:
from counter import Counter
Solution 2: use the backport_collections package.
Next patch (the import statement) the package you're getting exception at i.e. pandas in my case:
from backport_collections import Counter

You're probably using an old version of Python, the Counter class, as stated in the documentation was added in version 2.7.

You should use a new version of python AS python 3. You can then use this module.
Then import,
import collections
from collections import counter

Related

Importing deprecated statements in Wikidata

I read that Openrefine Wikidata import can only create statements with rank = Normal.
I am in a situation where I would like to import deprecated identifiers, namely old ISSNs (identifiers for serial publications) that used to be used to refer to a publication, but that are no longer in use. They have value because other bibliographic databases might still use them to refer to a publication.
So I'd like to be able to import these values, and mark them as "deprecated".
OpenRefine Wikidata import really really can't do that ? any workaround I could use ?
If I can't do that by the time I import the data from OpenRefine, I can flag these statements with "reason for deprecation = withdrawn identifier value". Can I run another automated process after import that would automatically set a deprecated rank on these statements ?
If I can't set them automatically to deprecated neither at import time nor after, which other qualifier can I use to indicated these identifiers are no longer in use ? (the official term being "Cancelled ISSN")
Thanks
A possibility I found would be to use the Wikidata-Toolkit library, in which StatementBuilder can create Statements with a specified rank. I would still to know/fetch the list of statements to modify...
I am still open to suggestions on the possibility to specify ranks at import time.

Typescript typings for momentjs and moment-range not working together

I am using momentjs and moment-range with a Typescript, so I have install typings for moment-range from npm #types, and the typing for momentjs comes with it.
import * as moment from 'moment';
import 'moment-range';
...
private tableDatePeriod: moment.Range;
but on compile I am getting this error - [ts] Module 'moment' has no exported member 'Range'.
Try this
import * as moment from 'moment';
import { default as DateRange } from 'moment-range';
let Range=new DateRange(dayFrom, dayTo);
Range.toArray('days');
let DatesArray=[];
diffDatesArray.push(moment(some_prop_from_array._d).format("YYYY-MM-DD"));
The following works for me for versions:
moment: 2.17.1
moment-range: 2.2.0
#types/moment-range: 2.0.33
The problem is that moment-range.js does not export any new entity for the range functionality, it extends moment type with range functionalities. Therefore these functionalities should be imported from moment.
import {Moment, Range, range as RangeConstructor} from "moment";
This import loads the moment-range functionalities from moment. In this example I import Range interface and range factory method (to create/construct ranges).
That should be enough, but in case you are using some type of AMD or similar dependency, the moment-range module should be loaded (required using AMD terms).
The following is the hack that works for me:
import DateRange = require("moment-range");
DateRange;
The first import includes moment-rangemodule as a dependency of the current module, therefore it is loaded as required first.
The second line does nothing, but the TypeScript compiler removes unused dependencies, so the former imported dependency must be invoked in some way in order to avoid such compiler optimization.
In reference to your question, you can now instantiate the variable:
private tableDatePeriod: Range;
And initialize:
tableDatePeriod = RangeConstructor('2016-01-09', '2016-01-10');

What's the difference between these two imports statement?

In Meteor 1.4, what's the difference between these two import statements in the client code and in the server code? Why?
Client code:
import { Items } from '../imports/api/items.js';
Server code:
import '../imports/api/items.js';
Both statements will execute the file given, however the first one will add Item to the scope. This can be useful if the file has many exports and you are only concerned with that single class/function/object/etc.
The second is used for side effects provided from importing it (like initializing a store or something like that; I confess I don't know much about meteor).
Mozilla has a great resource on the import statement https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

How to make Flow understand code written for Node.js?

I'm just getting started with Flow, trying to introduce it into an existing Node codebase.
Here are two lines Flow complains about:
import Module from 'module';
const nodeVersion = Number(process.versions.node.split('.')[0]);
The warnings about these lines are, respectively:
module. Required module not found
call of method `split`. Method cannot be called on possibly null value
So it seems like Flow isn't aware of some things that are standard in a Node environment (e.g. process.versions.node is guaranteed to be a string, and there is definitely a Node builtin called module).
But then again, Flow's configuration docs suggest it's Node-aware by default. And I have plenty of other stuff like import fs from 'fs'; which does not cause any warning. So what am I doing wrong?
Module fs works as expected because Flow comes with built-in definitions for it, see declare module "fs" here: https://github.com/facebook/flow/blob/master/lib/node.js#L624
Regarding process.versions.node, you can see in the same file that the versions key is typed as a map of nullable strings, with no mention of the specific node property: versions : { [key: string] : ?string };. So you'll need to either make a PR to improve this definition, or adjust your code for the possibility of that value being null.
I guess the answer about module "module" is obvious now – there are no built-in definitions for that module in Flow in lib/node.js. You could write your own definitions, and optionally send a PR with them to the Flow team. You can also try searching github for these, someone might have done the work already.
That lib directory is very useful by the way, it has Flow definitions for DOM and other stuff as well.

Uncaught TypeError: Blaze.Var is not a function

This one is driving me a little crazy.
I am playing with blaze and trying to create simple increment decrement with it.
here is my working code in codepen
http://codepen.io/distalx/pen/rVbQmm?editors=100
But when i am implementing it with meteor it gives me this error at following line
var counter = new Blaze.Var(0);
'Uncaught TypeError: Blaze.Var is not a function.'
here is meteorpad of it
http://meteorpad.com/pad/nhgMaMKNMQEainhcY/increment%20decrement
what am i doing wrong here.!!?
The Codepen you are referring to uses an old version of Blaze (as a standalone library) which seems to not be maintained or updated anymore (see discussions in the Issues list).
The version of Blaze used by Meteor is a package available here and it seems to differ a lot.
In fact, this version of Blaze does not have any .Var() method.
But you can have the same functionalities by using a ReactiveVar with the reactive-var package.

Resources