半城柳色半声笛

文章 评论 浏览 26

半城柳色半声笛 2024-11-25 11:46:00

如果您使用的是 SQL 数据库,您是否考虑过从数据库自动生成数据实体模型 (.edmx)?然后您可以使用那里生成的所有类并避免整个混乱。

If you are using a SQL database, have you considered generating a data entity model (.edmx) from the database automatically? Then you can work with all the classes generated there and avoid this whole mess.

如何将 linq var 数据类型传递给方法?

半城柳色半声笛 2024-11-25 08:10:22

除非您是 TCP/IP 专家并了解其全部影响,否则不要设置 NoDelay。如果你还没读过史蒂文斯的书,就别想了。

这是一个示例问题:如果建立套接字连接并在其上发送 8 个字节,那么这 8 个字节是立即发送还是 Nagle 算法等待更多数据发送?答案是“立即发送 8 个字节” - 但在您确切理解为什么为什么这就是答案之前,不要考虑搞乱 Nagle。

这里还有一个问题:在标准命令/响应协议中,每个数据包应用多少 Nagle 延迟?答案是:没有。同样,您应该研究 Nagle 在这种常见情况下为什么导致没有延迟。

如果您没有看到 250 毫秒(Nagle 在最坏的情况下造成的最大延迟)发送的数据,那么还有其他问题。

Don't set NoDelay unless you are an expert at TCP/IP and understand its full ramifications. If you haven't read Stevens, don't even think about it.

Here's an example question: if you establish a socket connection and send 8 bytes on it, are the 8 bytes immediately sent or does the Nagle algorithm wait for more data to send? The answer is "the 8 bytes are sent immediately" - but don't consider messing with Nagle until you understand exactly why that is the answer.

Here's another question: in a standard command/response protocol, how much Nagle delay is applied to each packet? The answer: none. Again, you should research why Nagle causes no delays in this common scenario.

If you're not seeing the data sent by 250 milliseconds (the maximum delay caused by Nagle in the worst possible scenario), then there is something else wrong.

强制 TCP 流发送缓冲区内容

半城柳色半声笛 2024-11-25 04:50:53
$this->invites = Doctrine_Query::create()
    ->from('Utilisateur u')
    ->LeftJoin('u.Invites i ON i.utilisateur_id = u.id')
    ->where('u.Invites.invitation_id = ?', $this->invitation->getId())
    ->execute();
$this->invites = Doctrine_Query::create()
    ->from('Utilisateur u')
    ->LeftJoin('u.Invites i ON i.utilisateur_id = u.id')
    ->where('u.Invites.invitation_id = ?', $this->invitation->getId())
    ->execute();

Doctrine sql查询,不考虑where子句

半城柳色半声笛 2024-11-25 02:26:03

我正在使用您提供的示例数据文件作为示例。 TEXTSCAN 函数用于解析文件

data.dat

0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F
2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6
3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1
0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F
2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6
3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

MATLAB

%# parse data file
fid = fopen('data.dat','rt');
C = textscan(fid, [repmat('%f ',[1 9]) '%s %s'], 'CollectOutput',true);
fclose(fid);

%# extract and reshape numeric data
M = C{1};
M = reshape(M', size(M,2)*3, [])';   %# similar to 'Michael J. Barber' answer

%# extract textual data
T = C{2}(1:3:end,:);

%# we can merge all into one cell array
data = [T num2cell(M)];

代码由于数据包含异构类型(数字和字符),因此我们单独读取和存储它们。最后一行代码显示了将所有数据合并到单个元胞数组中的一种方法:

data = 
    'E'    'F'    [0]    [2.3000]    [4.5000]    [0.9000]    [0.5000]    [3.4000]    [0]    [0.3000]    [0.5000]    [2.9000]    [5.4000]    [7.2000]    [4.8000]    [3.7000]    [9.1000]    [2.3000]    [4.1000]    [5.6000]    [3.4000]    [6.1000]    [4.8000]    [6.4000]    [0.4000]    [0.6000]    [0.3000]    [5.4000]    [7.1000]
    'E'    'F'    [0]    [2.3000]    [4.5000]    [0.9000]    [0.5000]    [3.4000]    [0]    [0.3000]    [0.5000]    [2.9000]    [5.4000]    [7.2000]    [4.8000]    [3.7000]    [9.1000]    [2.3000]    [4.1000]    [5.6000]    [3.4000]    [6.1000]    [4.8000]    [6.4000]    [0.4000]    [0.6000]    [0.3000]    [5.4000]    [7.1000]

I am using the sample data file you've given as an example. The TEXTSCAN function is used to parse the file

data.dat

0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F
2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6
3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1
0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F
2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6
3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

MATLAB code

%# parse data file
fid = fopen('data.dat','rt');
C = textscan(fid, [repmat('%f ',[1 9]) '%s %s'], 'CollectOutput',true);
fclose(fid);

%# extract and reshape numeric data
M = C{1};
M = reshape(M', size(M,2)*3, [])';   %# similar to 'Michael J. Barber' answer

%# extract textual data
T = C{2}(1:3:end,:);

%# we can merge all into one cell array
data = [T num2cell(M)];

Note that since the data contains heterogeneous types (numeric and characters), we read and store them separately. The final line of code shows one way of merging all data into a single cell array:

data = 
    'E'    'F'    [0]    [2.3000]    [4.5000]    [0.9000]    [0.5000]    [3.4000]    [0]    [0.3000]    [0.5000]    [2.9000]    [5.4000]    [7.2000]    [4.8000]    [3.7000]    [9.1000]    [2.3000]    [4.1000]    [5.6000]    [3.4000]    [6.1000]    [4.8000]    [6.4000]    [0.4000]    [0.6000]    [0.3000]    [5.4000]    [7.1000]
    'E'    'F'    [0]    [2.3000]    [4.5000]    [0.9000]    [0.5000]    [3.4000]    [0]    [0.3000]    [0.5000]    [2.9000]    [5.4000]    [7.2000]    [4.8000]    [3.7000]    [9.1000]    [2.3000]    [4.1000]    [5.6000]    [3.4000]    [6.1000]    [4.8000]    [6.4000]    [0.4000]    [0.6000]    [0.3000]    [5.4000]    [7.1000]

如何按以下方式操作数据?

半城柳色半声笛 2024-11-24 20:12:49

[编辑] 函数式方法(需要 Ruby 1.9):

xs = 3.upto(5).flat_map do |length|
  [1, 2, 3, 4, 5].repeated_permutation(length).select do |permutation|
    permutation.include?(5)
  end  
end
xs.size # 2531

[edit] Functional approach (requires Ruby 1.9):

xs = 3.upto(5).flat_map do |length|
  [1, 2, 3, 4, 5].repeated_permutation(length).select do |permutation|
    permutation.include?(5)
  end  
end
xs.size # 2531

如何使用 ruby​​ 生成数字组合?

半城柳色半声笛 2024-11-24 19:35:36

这取决于你想要什么。要获得以毫秒为单位的差异,请说dtStartDate.getTime() - dtEndDate.getTime()

要获得天、小时等的差异,请使用 Calendar 类(after、before、compareTo、roll 等方法可能会有所帮助)。

It depends what do you want. To get difference in milliseconds say dtStartDate.getTime() - dtEndDate.getTime().

To get difference in days, hours etc use Calendar class (methods like after, before, compareTo, roll may help).

如何在Java中对日期进行算术运算?

半城柳色半声笛 2024-11-24 17:10:21

看起来您正在使用“X,Y”来创建您的观点。
从文本创建点时,请改用“Y, X”。

看看这个MSDN 文章 了解更多信息。

It looks like you're creating your point using 'X, Y'.
When creating a point from text, use 'Y, X' instead.

Check out this MSDN Article for some more info.

Ms SQL geography.STDistance 返回错误的距离

半城柳色半声笛 2024-11-24 10:56:29

尽管可以使用 EnumBuilder 来自数据库值,它在编译时没有任何价值。

有关替代方案,请参阅这篇文章。

Although it would be possible to create an Enum at runtime using EnumBuilder from your database values, it would not be of any value at compile time.

See this article on an alternative.

有没有办法从数据库表动态填充枚举?

半城柳色半声笛 2024-11-24 10:25:56

我注意到 div.class 可以处理这种样式,而 .class div 则不能。

div.class 查找存在 class 类的 div

.class div 查找作为具有 class 类的元素的后代的 div

您的元素是一个带有 class 类的 div ,因此选择器不会选择它。

此外,.class 还可以处理样式。

.class 将选择具有该类的任何元素,包括任何 div 元素。

I notice that div.class handles this style, while .class div does not.

div.class looks for a div with the class class present.

.class div looks for a div that is a descendent of an element with the class class.

Your element is a div with the class class, hence the selector does not select it.

Moreover, .class handles the style as well.

.class will select any element with that class, including any div elements.

“div .class”、“.class”之间的区别和'.class div'

半城柳色半声笛 2024-11-24 01:06:25

因为微软在 IE7 之后放弃了“过滤器”,所以我很惊讶你说它可以在 IE9 中工作。

Because Microsoft dumped 'filter' after IE7 so I'm surprised you say it works in IE9.

画布上的 IE8 模糊滤镜

半城柳色半声笛 2024-11-24 00:11:32

在任何时候,您的应用程序都会有(大量)活动对象,即使在收到内存警告(以及操作系统随后的内存恢复)之后也是如此。因此,您经常会看到许多这样的 malloc。

它们本身并不表示内存分配有问题,而可能只是表明您的程序正在运行。

另请参阅SO 主题,了解有关对象分配工具的更多信息

此外,您可以使用许多高级技术来检测内存分配问题。
在这里您可以找到一个很棒的教程,它将让您超越 Leaks 工具所允许的范围。

编辑:

关于这些 malloc 的确切含义,您必须认为您可以分配两大类对象(粗略地说):通过 Obj-C 运行时系统创建的 Objective-C 对象和“普通”C对象,通过 malloc 分配。

第二类的许多对象是由系统库和编译器 C 库(例如套接字或文件句柄等)分配的(无需直接调用 malloc)。这些 (C) 对象没有与之关联的类型信息,因此 Instruments 只是向您显示分配的内存块的大小,而没有更多可用信息。

很多时候,malloc 对象是由更高级别的类创建的,因此当您恢复与其实例关联的内存时,通过 malloc 分配的内存也会被释放。

您不应该特别担心它们,除非您看到它们的总体大小随着程序执行“无限增长”。在这种情况下,您需要首先研究分配/释放更高级别对象的方式,并了解代码中的哪些地方陷入困境。

At any time, your app will have a (huge) number of living objects, even after getting a memory warning (and the subsequent memory recovery by the operating system). So, it is pretty common that you will also see many of those mallocs you are seeing.

They are not in themselves a sign that something is wrong with memory allocation, but possibly only of the fact that your program is running.

Also have a look at this S.O. topic to learn more about the object allocation tool.

Furthermore, there are many advanced techniques you can use to detect memory allocation problems.
Here you can find a great tutorial that will allow you to go way beyond what the Leaks tool allows you to.

EDIT:

About the exact meaning of those mallocs, you have to think that you can allocate two broad classes of objects (to put it roughly): Objective-C objects that are created through the Obj-C runtime system, and "normal" C objects, that are allocated through malloc.

Many object of the second class are allocated (without you directly calling malloc) by system libraries and by the compiler C library (think about, e.g., sockets or file handles, whatever). Those (C) objects do not have type information associated to them, so Instruments simply shows you the size of the allocated memory block, without having more information available.

Many times malloc objects are created by higher-level classes, so that when you recover memory associated to their instances, also memory allocated through malloc is freed.

You should not worry specifically about them, unless you see that their overall size "grows indefinitely" along program execution. In such case you need first to investigate the way you alloc/release your higher level objects and understand where in your code things get stuck.

iPhone/Instruments:“malloc”是什么?对象摘要中的条目?

半城柳色半声笛 2024-11-24 00:06:38

Devise 的旧版本 (<1.4.2) 对 to_json 和 to_xml 方法执行了 Monkeypatch,覆盖了 :only =>; [] 选项以及 attr_accessible 中定义的属性。恼人的。

现在这已被更改,因此serialized_hash 被覆盖,并且任何 :only =>; to_json 或 to_xml 中设置的 [:attribute] 选项将被保留。

就我而言,我最终自己对 to_json 进行了猴子修补,并向所有 ActiveRecord 模型添加了 api_accessible 方法。

class ActiveRecord::Base

  def to_json(options = {}, &block)
    if self.class.respond_to?(:api_attributes)
      super(build_serialize_options(options), &block)
    else
      super(options, &block)
    end
  end

  class << self
    attr_reader :api_attributes
    def api_accessible(*args)
      @api_attributes ||= []
      @api_attributes += args
    end
  end

  private

    def build_serialize_options(options)
      return options if self.class.api_attributes.blank?
      methods = self.class.instance_methods - self.class.attribute_names.map(&:to_sym)
      api_methods = self.class.api_attributes.select { |m| methods.include?(m) }
      api_attrs = self.class.api_attributes - api_methods
      options.merge!(only: api_attrs) if api_attrs.present?
      options.merge!(methods: api_methods) if api_methods.present?
      return options
    end

end

这意味着您现在可以定义在调用 to_json 时默认公开的属性(和方法!)列表。 Respond_with 还使用 to_json,因此它非常适合 API。

例如,user.rb

class User < ActiveRecord::Base

 devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable

  #Setup accessible (or protected) attributes for your model
  attr_accessible :email,
                  :password,
                  :password_confirmation,
                  :remember_me,
                  :first_name,
                  :last_name,


  api_accessible :id,
                 :name,
                 :created_at,
                 :first_name,
                 :last_name,
                 :some_model_method_also_works
end

Older versions ( < 1.4.2) of Devise performed a monkeypatch on the to_json and to_xml methods, overwriting the :only => [] option with the attributes defined in attr_accessible. Annoying.

This has now been changed, so that serializable_hash is overwritten instead, and any :only => [:attribute] options set in to_json or to_xml are persisted.

In my case, I ended up monkeypatching to_json myself, and adding a method api_accessible to all ActiveRecord models.

class ActiveRecord::Base

  def to_json(options = {}, &block)
    if self.class.respond_to?(:api_attributes)
      super(build_serialize_options(options), &block)
    else
      super(options, &block)
    end
  end

  class << self
    attr_reader :api_attributes
    def api_accessible(*args)
      @api_attributes ||= []
      @api_attributes += args
    end
  end

  private

    def build_serialize_options(options)
      return options if self.class.api_attributes.blank?
      methods = self.class.instance_methods - self.class.attribute_names.map(&:to_sym)
      api_methods = self.class.api_attributes.select { |m| methods.include?(m) }
      api_attrs = self.class.api_attributes - api_methods
      options.merge!(only: api_attrs) if api_attrs.present?
      options.merge!(methods: api_methods) if api_methods.present?
      return options
    end

end

This means that you can now define a list of attributes (and methods!) that will be exposed by default when calling to_json. Respond_with also uses to_json, so it works well for APIs.

Eg, user.rb

class User < ActiveRecord::Base

 devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable

  #Setup accessible (or protected) attributes for your model
  attr_accessible :email,
                  :password,
                  :password_confirmation,
                  :remember_me,
                  :first_name,
                  :last_name,


  api_accessible :id,
                 :name,
                 :created_at,
                 :first_name,
                 :last_name,
                 :some_model_method_also_works
end

使用设计和响应_with Rails 3 中的 attr_accessible

半城柳色半声笛 2024-11-23 23:54:13

该算法的时间复杂度和过程复杂度都是O(braindead)

  • 数据集中有足够大的,您将等待答案,直到太阳爆炸(264 秒相当于 5000 亿年多一点)。
  • 如果数据集大小足够大,您将 (a) 达到进程限制; (b) 发现早睡会在晚睡开始之前完成,这意味着集合 (2,9,9,9,9,9,...,9,9,1) 将无法正确排序 12

在这种情况下,时间复杂度是无关紧要的。您不可能得到比“错误”更差的优化。随着数据集大小的变化,可以使用复杂性分析来比较算法,但当算法一开始就很荒谬时就不行了:-)

Both the time complexity and the process complexity of that algorithm are O(braindead):

  • With a sufficiently large value in the data set, you'll be waiting for an answer until the sun explodes (264 seconds is a little over half a trillion years).
  • With a sufficiently large data set size, you'll (a) hit your process limit; and (b) find that early sleeps will finish before the latter ones begin, meaning the set (2,9,9,9,9,9,...,9,9,1) will not sort the 1 and 2 correctly.

Time complexity is irrelevant in this case. You can't get any less optimised than "wrong". It's okay to use complexity analysis to compare algorithms as the data set size changes, but not when the algorithms are ludicrous in the first place :-)

睡眠排序的时间复杂度是多少?

半城柳色半声笛 2024-11-23 23:08:04

您必须在分号后添加空格,所以最好的方法是:

$cookie = array();
foreach( $_COOKIE as $key => $value ) {
  $cookie[] = "{$key}={$value}";
};

$cookie = implode('; ', $cookie);

$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_COOKIE, $cookie);

you have to add space after semicolon so the best way is:

$cookie = array();
foreach( $_COOKIE as $key => $value ) {
  $cookie[] = "{$key}={$value}";
};

$cookie = implode('; ', $cookie);

$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_COOKIE, $cookie);

PHP Curl 和 setcookie 问题

我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文