小编vas*_*vas的帖子

运行时错误:__class__ 未设置将“AbstractBaseUser”定义为 <class 'django.contrib.auth.base_user.Abstract BaseUser'>。__classcell__ 是否被传播

  1. 在为项目手动设置PostgreSQL 数据库后尝试迁移数据库时出现此错误。
  2. 当这个错误出现时,我试图运行从 github 分叉的克隆 quora 项目

遵循完整的错误描述:

RuntimeError: __class__ not set defining 'AbstractBaseUser' as <class 'django.contrib.auth.base_user.Abstract BaseUser'>. Was __classcell__ propagated to type.__new__?
Run Code Online (Sandbox Code Playgroud)

跟踪跟踪:

    Traceback (most recent call last):
      File "manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
          File "C:\Users\user\Desktop\quora-clone-master\env\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
            utility.execute()
          File "C:\Users\user\Desktop\quora-clone-master\env\lib\site-packages\django\core\management\__init__.py", line 327, in execute
            django.setup()
          File "C:\Users\user\Desktop\quora-clone-master\env\lib\site-packages\django\__init__.py", line 18, in setup
            apps.populate(settings.INSTALLED_APPS)
          File "C:\Users\user\Desktop\quora-clone-master\env\lib\site-packages\django\apps\registry.py", line 108, in populate
            app_config.import_models(all_models)
          File "C:\Users\user\Desktop\quora-clone-master\env\lib\site-packages\django\apps\config.py", line 202, in import_models
            self.models_module = import_module(models_module_name)
          File …
Run Code Online (Sandbox Code Playgroud)

python django django-models

9
推荐指数
2
解决办法
7406
查看次数

当看似相同的三元运算符构造没有时,为什么使用“if-else”语句会产生 TypeScript 编译器错误?

我有一个函数,旨在返回值IDBValidKey或转换为IDBValidKey. 如果我使用三元运算符编写函数,它可以正常工作,但如果我将其编写为 if-else 语句,则会导致编译器错误:

interface IDBValidKeyConvertible<TConverted extends IDBValidKey> {
    convertToIDBValidKey: () => TConverted;
}

function isIDBValidKeyConvertible<TConvertedDBValidKey extends IDBValidKey>(object: unknown): object is IDBValidKeyConvertible<TConvertedDBValidKey> {
    return typeof((object as IDBValidKeyConvertible<TConvertedDBValidKey>).convertToIDBValidKey) === "function";
}

type IDBValidKeyOrConverted<TKey> = TKey extends IDBValidKeyConvertible<infer TConvertedKey> ? TConvertedKey : TKey;

function getKeyOrConvertedKey<TKey extends IDBValidKey | IDBValidKeyConvertible<any>>(input: TKey): IDBValidKeyOrConverted<TKey> {
    if (isIDBValidKeyConvertible<IDBValidKeyOrConverted<TKey>>(input)) {
        return input.convertToIDBValidKey();
    } else {
        return input;
    }
}

function getKeyOrConvertedKeyTernary<TKey extends IDBValidKey | IDBValidKeyConvertible<any>>(input: TKey): IDBValidKeyOrConverted<TKey> {
    return (isIDBValidKeyConvertible<IDBValidKeyOrConverted<TKey>>(input)) ? input.convertToIDBValidKey() : …
Run Code Online (Sandbox Code Playgroud)

typescript typescript-generics

8
推荐指数
1
解决办法
216
查看次数

优化用 TypeScript 编写的文件内容解析器类

I got a typescript module (used by a VSCode extension) which accepts a directory and parses the content contained within the files. For directories containing large number of files this parsing takes a bit of time therefore would like some advice on how to optimize it.

I don't want to copy/paste the entire class files therefore will be using a mock pseudocode containing the parts that I think are relevant.

class Parser {
    constructor(_dir: string) {
        this.dir = _dir;
    } …
Run Code Online (Sandbox Code Playgroud)

javascript parallel-processing node-worker-threads

7
推荐指数
1
解决办法
122
查看次数

为什么 TypeScript 导入 CommonJS 而不是 ESM

我按照本教程创建了一个双包。包文件结构如下所示:

my-awesome-lib
  package.json
  dist
  |-- mjs
    |-- package.json
    |-- index.js
  |-- cjs
    |-- package.json
    |-- index.js
Run Code Online (Sandbox Code Playgroud)

package.json

  "main": "dist/cjs/index.js",
  "module": "dist/mjs/index.js",
  "exports": {
    ".": {
      "require": "./dist/cjs/index.js",
      "import": "./dist/mjs/index.js"
    }
  },
Run Code Online (Sandbox Code Playgroud)

文件夹package.json中的mjs

{
    "type": "module"
}
Run Code Online (Sandbox Code Playgroud)

文件夹package.json中的cjs

{
    "type": "commonjs"
}
Run Code Online (Sandbox Code Playgroud)

在我的 Typescript 应用程序中,我安装了自己的包,并尝试导入它。然而,我观察到一件奇怪的事情:

// src/index.ts
import { Class } from 'my-awesome-lib'
import { Class } from '../node_modules/my-awesome-lib/dist/mjs/index.js';
Run Code Online (Sandbox Code Playgroud)

第一个导入语句指向而../node_modules/my-awesome-lib/dist/cjs/index.js不是mjs文件夹。第二个导入语句是我真正想要的。

谁能告诉我出了什么问题吗?是因为我以错误的方式发布了我的库吗?


附注

这是我的 tsconfig.json

{ …
Run Code Online (Sandbox Code Playgroud)

commonjs node.js typescript es6-modules

7
推荐指数
1
解决办法
6633
查看次数

使用 SIMD 将 ascii 字符串位打包为 7 位二进制 blob

相关:bitpack ascii string into 7-bit binary blob using ARM-v8 Neon SIMD - 同样的问题专门针对 AArch64 内在函数。这个问题涵盖了可移植的 C 和 x86-64 内在函数。


我想将 char 字符串编码为 7 位 blob,以减少 12.5% 的内存。我想尽可能快地完成它,即在编码大字符串时以最小的延迟。

这是该算法的简单实现:

void ascii_pack(const char* ascii, size_t len, uint8_t* bin) {
  uint64_t val;
  const char* end = ascii + len;

  while (ascii + 8 <= end) {
    memcpy(&val, ascii, 8);
    uint64_t dest = (val & 0xFF);

    // Compiler will perform loop unrolling
    for (unsigned i = 1; i <= 7; ++i) {
      val …
Run Code Online (Sandbox Code Playgroud)

c ascii sse simd intrinsics

7
推荐指数
1
解决办法
417
查看次数

map()、reduce() 和 filter 与 forEach()

刚刚学习MapReduce,所以想知道这样写有什么好处吗

const initialValue = 0;

if (this.items) {
  return this.items.filter(function (item) {
    return item && item.quantity && item.price;
  }).reduce(function(previousValue, currentValue) {
    return previousValue + currentValue.quantity * currentValue.price ;
  }, initialValue);
} else {
  return initialValue;
}
Run Code Online (Sandbox Code Playgroud)

而不是仅仅

let total = 0;
if (this.items) {
  this.items.forEach(function(item) {
    if (item && item.quantity && item.price) {
      total += item.quantity * item.price;
    }
  });
}
return total;
Run Code Online (Sandbox Code Playgroud)

javascript performance functional-programming mapreduce

6
推荐指数
2
解决办法
5003
查看次数

可以将position:absolute元素设为粘性吗?

在 CSS 中,position: sticky使元素能够以某种position: static行为进行显示(即,它采用其在文档流中的默认位置),直到它到达某个滚动位置,然后采用某种position: fixed行为。

position: sticky那么...这是否意味着我们不能在需要正常行为的元素上使用position: absolute


语境:

我有一个流出元素,它占据了视口左上角的位置。滚动一两英寸后,元素会到达视口的顶部,理想情况下,我希望它不会在此时继续消失。

css css-position sticky

6
推荐指数
2
解决办法
8608
查看次数

如何解决导入本地Go模块的"无法找到路径X的模块"?

在我的Go项目中,我想将一些通用功能分解为Go模块,与主项目分开.我在GOPATH之外这样做是为了与go的未来保持一致.我不想在GitHub或其他任何地方发布模块.

我将此模块导入主项目的所有尝试都会导致:

cannot find module for path X
Run Code Online (Sandbox Code Playgroud)

我已经go mod init X在模块的文件夹中运行了.其go.mod文件的内容是:

module X
Run Code Online (Sandbox Code Playgroud)

构建或安装此模块似乎什么都不做.我没有发现它的迹象$GOPATH/pgk/mod.

我尝试了各种import语句:

救命!

go vgo

6
推荐指数
2
解决办法
2776
查看次数

根据条件查找所有索引

如何根据对象数组的条件获取所有索引?我已经尝试了下面的代码,但它只返回第一次出现的情况。

a = [
  {prop1:"abc",prop2:"yutu"},
  {prop1:"bnmb",prop2:"yutu"},
  {prop1:"zxvz",prop2:"qwrq"}];
    
index = a.findIndex(x => x.prop2 ==="yutu");

console.log(index);
Run Code Online (Sandbox Code Playgroud)

javascript arrays vue.js

6
推荐指数
1
解决办法
2453
查看次数

当属性的类型是判别式时,为什么“typeof”不缩小联合类型?

如果联合类型的成员共享一个属性,并且该属性的类型可用于区分这些成员,那么我应该能够使用作为条件来缩小if子句中的类型范围typeof。但这不起作用。

例如,在if下面的子句中, 的类型event应推断为UserTextEvent, 的类型event.target应推断为HTMLInputElement

type UserTextEvent = { value: string, target: HTMLInputElement };
type UserMouseEvent = { value: [number, number], target: HTMLElement };

type UserEvent = UserTextEvent | UserMouseEvent 


function handle(event: UserEvent) {
  if (typeof event.value === 'string') {
    event.value  // string, as expected

    event.target // should be narrowed to HTMLInputElement, but
                 // is still HTMLInputElement | HTMLElement. Why?
  }
}
Run Code Online (Sandbox Code Playgroud)

type-inference discriminated-union narrowing typescript union-types

6
推荐指数
1
解决办法
650
查看次数